如果某些字符然后添加逗号

时间:2014-10-30 16:39:22

标签: string excel vba character

我需要一个宏来查找如下字符:"大写""小写""大写"

我试过这个:

If character like "[A-Z]", "[a-z]", "[A-Z]" then......

括号在不同的位置,它不起作用。

请让我知道我做错了什么

3 个答案:

答案 0 :(得分:2)

? "AcK" like "[A-Z][a-z][A-Z]" '>> True

? "ACK" like "[A-Z][a-z][A-Z]" '>> False

答案 1 :(得分:1)

使用公式可以吗?

=IF (  AND    (ISNUMBER(FIND((MID(B4,1,1)),$D$4,1)),
               ISNUMBER(FIND((MID(B4,2,1)),$D$5,1)),
               ISNUMBER(FIND((MID(B4,3,1)),$D$4,1))                       
              ),
       "True",
       "False")

,其中       单元格B4包含您正在测试的文本,例如," AcK"

  cell D4 contains the string "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

  cell D5 contains the string "abcdefghijklmnopqrstuvwxyz"

答案 2 :(得分:0)

如果我理解了你的目标,试着在你的项目中加入这样的函数:

Public Function editString(ByVal nativeString As String) As String

    Dim subStr As String, subStr2 As String

    For j = 1 To Len(nativeString)

        subStr = Mid(nativeString, j, 3)
        If subStr Like "[A-Z][a-z][A-Z]" Then
            subStr2 = Left(subStr, 2) & "," & Right(subStr, 1)
            nativeString = Replace(nativeString, subStr, subStr2)
        End If

    Next j

    editString = nativeString

End Function

...然后通过简单地将它们传递给函数来修改字符串对象:

Public Sub ModifyString()
    Dim newString As String: newString = editString("stAcKoverflow")
End Sub

你应该得到你想要的东西。