VBA找到非字符串

时间:2019-05-14 22:46:42

标签: vba string

我有一个带字符串的变量...并且我想知道它是否包含除单引号,逗号和空格(“',”)以外的任何值。我在excel中使用vba。

例如,我有一个变量strA =“'test','player'” 我想检查strA是否包含除“',”(单引号,逗号和空格)以外的其他字符。

谢谢

1 个答案:

答案 0 :(得分:0)

这是基于System的策略 我没有方便的vba,但这应该可以。这个想法是删除所有这些字符,看看是否还剩下什么。 text代表您正在测试的字符串。

Dim TempS As String
TempS = Replace(text, " " , "")
TempS = Replace(TempS, "," , "")
TempS = Replace(TempS, "'" , "")

您的结果是Len(TempS>0)

另一种方法是使用递归,如果字符串为空,则第一个字符为false,如果第一个字符是其余三个字符串中的一个,则调用递归,否则返回true。这是代码

 function hasOtherChars(s As String) As Boolean
 hasOtherChars=false
 if (len(s)=0) then
   exit function
 end if
 Dim asciiSpace As Integer
    asciiSpace = Asc(" ")
    Dim asciiComma As Integer
    asciiComma= Asc(",")
    Dim asciiApostrophe As Integer
    asciiApostrophe = Asc("'")
 Dim c as Integer
 c = Asc(Mid$(s, 1, 1))
 if ((c=asciiSpace) or (c=asciiComma) or (c=asciiApostrophe)) then
    hasOtherChars = hasOtherChars(Mid$(s,2))
else
 hasOtherChars=true
 end if
End function

我也是从另一个线程借来的。