字符串到vba中的yyyyMMdd

时间:2017-06-28 05:01:03

标签: vba excel-vba excel

我有以下可能的模式字符串,我想在 vba 转换为 yyyyMMdd (20160602) 。         我已经尝试过Format(" 20160602"," 00000000")。但它给出了00201662

'possible pattern
20160602
201662
2016062
2016602

与此同时,我不想要以下类型的长代码

Dim trgetYmdFrom As String

trgetYmdFrom = "2016602"
If Len(trgetYmdFrom) = 6 Then
    trgetYmdFrom  = Mid(trgetYmdFrom, 1, 4) & "0" & Mid(trgetYmdFrom, 5, 1) & "0" & Mid(trgetYmdFrom, 6, 1)
ElseIf Len(trgetYmdFrom) = 7 Then
    If Mid(trgetYmdFrom, 5, 1) >= 0 And Mid(trgetYmdFrom, 5, 1) <= 1 Then
        trgetYmdFrom  = Mid(trgetYmdFrom, 1, 4) & Mid(trgetYmdFrom, 5, 2) & "0" & Mid(trgetYmdFrom, 7, 1)
    Else
        trgetYmdFrom  = Mid(trgetYmdFrom, 1, 4) & "0" & Mid(trgetYmdFrom, 5, 1) & Mid(trgetYmdFrom, 6, 2)
    End If
ElseIf Len(trgetYmdFrom) = 8 Then
    trgetYmdFrom  = trgetYmdFrom
End If

我需要最短路才能将字符串转换为yyyyMMdd

注意:

  

在我的情况下,2016111应该被视为20161101

3 个答案:

答案 0 :(得分:0)

尝试:

trgetYmdFrom = Format(trgetYmdFrom, "yyyyMMdd")

编辑1

这就是我的意思。您可以尝试所有选项。

Dim trgetYmdFrom As String

trgetYmdFrom = "20160602"

If Len(trgetYmdFrom) = 6 Then
trgetYmdFrom = Mid(trgetYmdFrom, 1, 4) & "." & Mid(trgetYmdFrom, 5, 1) & "." & Mid(trgetYmdFrom, 6, 1)
    ElseIf Len(trgetYmdFrom) = 7 And Mid(trgetYmdFrom, 5, 1) = 0 Then
    trgetYmdFrom = Mid(trgetYmdFrom, 1, 4) & "." & Mid(trgetYmdFrom, 5, 2) & "." & Mid(trgetYmdFrom, 6, 1)
    ElseIf Len(trgetYmdFrom) = 7 And Mid(trgetYmdFrom, 6, 1) = 0 Then
    trgetYmdFrom = Mid(trgetYmdFrom, 1, 4) & "." & Mid(trgetYmdFrom, 5, 1) & "." & Mid(trgetYmdFrom, 6, 2)
    ElseIf Len(trgetYmdFrom) = 8 Then
    trgetYmdFrom = Mid(trgetYmdFrom, 1, 4) & "." & Mid(trgetYmdFrom, 5, 2) & "." & Mid(trgetYmdFrom, 7, 2)
End If

trgetYmdFrom = Format(trgetYmdFrom, "yyyymmdd")
MsgBox trgetYmdFrom

&#34;简化版&#34;

Dim s As String
s = "20160602"

If Len(s) = 6 Then
    s = Mid(s, 1, 4) & "." & Mid(s, 5, 1) & "." & Mid(s, 6, 1)
        ElseIf Len(s) = 7 And Mid(s, 5, 1) = 0 Then
    s = Mid(s, 1, 4) & "." & Mid(s, 5, 2) & "." & Mid(s, 6, 1)
        ElseIf Len(s) = 7 And Mid(s, 6, 1) = 0 Then
    s = Mid(s, 1, 4) & "." & Mid(s, 5, 1) & "." & Mid(s, 6, 2)
        ElseIf Len(s) = 8 Then
    s = Mid(s, 1, 4) & "." & Mid(s, 5, 2) & "." & Mid(s, 7, 2)
End If

s = Format(s, "yyyymmdd")

编辑3

Dim s As String
s = "2016111"

If Len(s) = 6 And Mid(s, 5, 1) >= 0 Then
    s = Mid(s, 1, 4) & "." & Mid(s, 5, 1) & "." & Mid(s, 6, 1)
        ElseIf Len(s) = 7 And Mid(s, 5, 1) <= 1 Then
    s = Mid(s, 1, 4) & "." & Mid(s, 5, 2) & "." & Mid(s, 7, 1)
        ElseIf Len(s) = 7 And Mid(s, 5, 1) > 1 Then
    s = Mid(s, 1, 4) & "." & Mid(s, 5, 1) & "." & Mid(s, 7, 1)
        ElseIf Len(s) = 7 And Mid(s, 6, 1) > 0 Then
    s = Mid(s, 1, 4) & "." & Mid(s, 5, 1) & "." & Mid(s, 6, 2)
        ElseIf Len(s) = 8 Then
    s = Mid(s, 1, 4) & "." & Mid(s, 5, 2) & "." & Mid(s, 7, 2)
End If

s = Format(s, "yyyymmdd")

答案 1 :(得分:0)

使用用户的功能是合适的。

Sub test()
    Dim trgetYmdFrom As String
    trgetYmdFrom = "2016602"
    trgetYmdFrom = ChangeFormat(trgetYmdFrom)
    Debug.Print trgetYmdFrom
End Sub


Function ChangeFormat(myDay As String)
    Dim y As String, m As String, d As String
    Select Case Len(myDay)
    Case 8
        y = Left(myDay, 4)
        m = Mid(myDay, 5, 2)
        d = Right(myDay, 2)
    Case 6
        y = Left(myDay, 4)
        m = Mid(myDay, 5, 1)
        d = Right(myDay, 1)
    Case 7
        If Mid(myDay, 5, 1) = "0" Then
            y = Left(myDay, 4)
            m = Mid(myDay, 5, 2)
            d = Right(myDay, 1)
        Else
            y = Left(myDay, 4)
            m = Mid(myDay, 5, 1)
            d = Right(myDay, 2)
        End If
    End Select
    ChangeFormat = Format(DateSerial(y, m, d), "yyyymmdd")

End Function

答案 2 :(得分:0)

花了很多时间在这个问题上。我无法获得任何简化的方法将字符串转换为yyyyMMdd。 因此,我将公式设置为与之前的长代码相同,以便将字符串转换为yyyyMMdd。最后的代码将如下所示。

<强>代码:

Dim trgetYmdFrom As String

Sub can_Click()

    stringToYYYYMMDD ("20160602")
    stringToYYYYMMDD ("201662")
    stringToYYYYMMDD ("2016062")
    stringToYYYYMMDD ("2016602")
    stringToYYYYMMDD ("2016111")
    stringToYYYYMMDD ("2016122")
End Sub

Sub stringToYYYYMMDD(s As String)

    Range("A1").Value = s
    Range("A2").Formula = "=IF(INT(LEN(A1))=6,LEFT(A1,4)&0&MID(A1,5,1)&0&MID(A1,6,1),IF(INT(LEN(A1))=7,LEFT(A1,4)&IF(INT(MID(A1,5,1))<=1,MID(A1,5,2)&0&MID(A1,7,1),0&MID(A1,5,1)&MID(A1,6,2)),A1))"
    trgetYmdFrom = Range("A2").Value
    Range("A1:A2").Clear

    'debug log
    Debug.Print trgetYmdFrom
End Sub

调试日志:

20160602
20160602
20160602
20160602
20161101
20161202