2015年10月2日 星期五

【Excel VBA】 中文日期轉成六位數字含斜線表示法。YYY年M月D日 轉成 YYY/0M/0D

Dim tempposY As String
Dim tempposM As String
Dim tempposD As String
Dim textY As String
Dim textM As String
Dim textD As String    
'目的 將國字的年月 轉成 6未數的表示  104年3月5日 => 104/03/05
'假設字串為。小心這邊字串最後沒有日。有的話下面稍微調整一下即可

retval="104年3月5"

 '看看裡面有沒有 年 月
    If (InStr(retval, "年")) Then


        tempposY = InStr(retval, "年")
        tempposM = InStr(retval, "月")
        tempposD = InStr(retval, "日")


        MsgBox Len(retval)
       MsgBox tempposY & " " & tempposM & " " & tempposD


        textY = Left(retval, tempposY - 1)
        textM = Mid(retval, tempposY + 1, tempposM - tempposY - 1)
        textD = Mid(retval, tempposM + 1, tempposD - tempposM - 1)

        If Len(textM) < 2 Then

            textM = "0" + textM

        End If
        If Len(textD) < 2 Then

            textD = "0" + textD

        End If


'        MsgBox textY
'        MsgBox textM
'        MsgBox textD

        retval = textY + "/" + textM + "/" + textD

        'MsgBox retval
   

endif

沒有留言:

張貼留言