为什么我的Replace()方法无法正常工作?

时间:2016-08-01 20:57:51

标签: vba excel-vba excel

我的UserForm包含一系列CheckBoxesTextBoxesButtons。一个TextBox会询问用户特定日期,一旦点击OKButton,它就会检查并检查用户的日期格式。我使用Select/Case来检查用户输入的不同格式,并将它们转换为特定格式以用于文件名目的(此宏获取用户选择的文件路径并生成报告,保存它作为[MM-DD-YY] Balance Report.xlsx

我的选择案例会检查用户的输入是否格式化为MMDDYYYY,MMDDYY,MM / DD / YYYY,MM / DD / YY,MM-DD-YYYY,MM-DD-YYYY。

代码如下:

Function PostDateBoolean() As Boolean

    PostingDate = PostDateTextBox.Value 'tested as 071616       (07-16-2016)

    Select Case True
        Case Is = PostingDate Like "##/##/##" Or _
            PostingDate Like "##-##-##" Or _
            PostingDate Like "##/##/20##" Or _
            PostingDate Like "##-##-20##" Or _
            PostingDate Like "######" Or _
            PostingDate Like "########"

            PostDateBoolean = True
        Select Case True
            Case Is = PostingDate Like "##/##/##" Or _
                PostingDate Like "##-##-##"
                PostingDate = Replace(PostingDate, Right(PostingDate, 2), _
                "20" & Right(PostingDate, 2))
                PostDateTextBox.Value = PostingDate
        End Select
        Select Case True
            Case Is = PostingDate Like "######"
                ' THE ERROR IS HERE VVVV
            PostingDate = Replace(PostingDate, Right(PostingDate, 2), _
                    "/20" & Right(PostingDate, 2))
                PostingDate = Replace(PostingDate, Right(PostingDate, 7), _
                    "/" & Right(PostingDate, 7))
                PostDateTextBox.Value = PostingDate
                GoTo CheckDone:
        End Select
        Select Case True
            Case Is = PostingDate Like "########"
                PostingDate = Replace(PostingDate, Right(PostingDate, 4), _
                    "/" & Right(PostingDate, 4))
                PostingDate = Replace(PostingDate, Right(PostingDate, 7), _
                    "/" & Right(PostingDate, 7))
                PostDateTextBox.Value = PostingDate
                GoTo CheckDone:
        End Select
CheckDone:
        Case Else
            msg = "Please Enter a Date in the correct Date Format:" & vbCr _
            & "MM/DD/YYYY Or MM/DD/YY"
        PostDateBoolean = False
    End Select
    PostDateFileName = PostingDate
    PostDateFileName = Replace(PostDateFileName, Right(PostDateFileName, 5), _
                "-" & Right(PostDateFileName, 4))
    PostDateFileName = Replace(PostDateFileName, Right(PostDateFileName, 8), _
                "-" & Right(PostDateFileName, 7))

End Function

在我的行之后

PostingDate = Replace(PostingDate, Right(PostingDate, 2), "/20" & Right(PostingDate, 2))

发布日期的值从07161607/2016/2016。上述代码行的意图是替换071616中的最后两位数字,并将其替换为/2016。出于某种原因,它取代了4位数而不仅仅是两位数。我假设这个问题是在Replace ( string1, find, replacement, [start, [count, [compare]]] ) [Find as String]的第二个参数中,它找到字符串中的所有值(16)并用/2016替换它。是否有更好的方法来获取JUST最后两个字符并替换它们,而不是字符串中的所有16's

2 个答案:

答案 0 :(得分:4)

当您不知道比赛的位置时,请使用th, td { border: 1px solid black; } tbody .colored td[rowspan]:not(:first-child) { border-left: 2px solid red; border-top: 2px solid red; border-bottom: 2px solid red; } tbody .colored td:nth-of-type(n+2) { border-top: 2px solid red; } tbody tr td:last-child { border-right: 2px solid red; } tbody tr:last-child td:nth-child(n+1) { border-bottom: 2px solid red; } 当您确切知道要替换的字符时,可以使用其他内容(ReplaceLeftRight)。

Mid

然而,更好的方法是,一旦您检测到日期的格式,请将其转换为实际的PostingDate = Left$(PostingDate, Len(PostingDate) - 2) & "/20" & Right$(PostingDate, 2) ,然后将Date转换为您喜欢的任何格式。

答案 1 :(得分:2)

这不是"答案"对你的问题,但是作为评论包含的时间太长了。

除了GSerg所说的,你的Select Case语句应该被重写为:

    PostDateBoolean = True

    Select Case True
        Case PostingDate Like "##/##/##",
             PostingDate Like "##-##-##"

            PostingDate = Left$(PostingDate, 6) & _
                          "20" & Right$(PostingDate, 2)
            PostDateTextBox.Value = PostingDate

        Case PostingDate Like "##/##/20##",
             PostingDate Like "##-##-20##"

        Case PostingDate Like "######"

            PostingDate = Left$(PostingDate, 2) & "/" & _
                          Mid$(PostingDate, 3, 2) & "/" & _
                          "20" & Right$(PostingDate, 2)
            PostDateTextBox.Value = PostingDate

        Case PostingDate Like "########"

            PostingDate = Left$(PostingDate, 2) & "/" & _
                          Mid$(PostingDate, 3, 2) & "/" & _
                          Right$(PostingDate, 4)
            PostDateTextBox.Value = PostingDate

        Case Else
            msg = "Please Enter a Date in the correct Date Format:" & vbCr _
            & "MM/DD/YYYY Or MM/DD/YY"
            PostDateBoolean = False
    End Select