Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VBA:匹配多个字符串_Vba - Fatal编程技术网

VBA:匹配多个字符串

VBA:匹配多个字符串,vba,Vba,我的问题最好理解为下面的示例,我的目标是,如果字符串与这些类别中定义的任何一个字符串匹配,则将以下字符串分类为类别。比如说, dim test_str as string test_str = "tomato" 如果测试字符串tomato与(1)potato,(2)tomato和(3)spaghetti中的任何一个关键字匹配,则番茄将被归类为食品 我现在有一种非常低效的方法,它涉及到使用多个strcomp,即 if(strcomp(test_str, "potato", vbtextcomp

我的问题最好理解为下面的示例,我的目标是,如果字符串与这些类别中定义的任何一个字符串匹配,则将以下字符串分类为类别。比如说,

dim test_str as string

test_str = "tomato"
如果测试字符串
tomato
与(1)
potato
,(2)
tomato
和(3)
spaghetti
中的任何一个关键字匹配,则番茄将被归类为食品

我现在有一种非常低效的方法,它涉及到使用多个strcomp,即

if(strcomp(test_str, "potato", vbtextcompare) = 0 or _
strcomp(test_str, "tomato", vbtextcompare) =0 or _ 
strcomp(test_str, "spaghetti", vbtextcompare)=0 ) then 
     'label test str as "food"

然而,如果我在“food”中定义了10个关键字,那么我需要10个strcomp语句,这将是一件乏味的事情。有更好的方法吗?

编写一个函数来帮助您

Function ArrayWordNotInText(textValue, arrayKeyword)
   Dim i
   ArrayWordNotInText = -1
   For i = LBound(arrayKeyword) To UBound(arrayKeyword)
      If Not StrComp(textValue, arrayKeyword(i), vbTextCompare) Then ArrayWordNotInText = i
   Next i
End Function

如果返回值=-1。。。不匹配,>0单词的索引

我只需将所有组合存储在一个字符串中,并检查值是否存在于
InStr

Const food = "|potato|tomato|spaghetti|"

Dim test_str As String
test_str = "tomato"

If InStr(1, food, "|" & test_str & "|", vbTextCompare) Then
  Debug.Print "food"
Else
  Debug.Print "not food"
End If

这是我第一次发帖;请原谅我的格式化。使用VBA的时间不长,但能够将其组合在一起

  Sub vinden4()

  Dim EXCEPT() As String, a As Integer

  EM = "no.replynoreply@ziggo.nl"

  Exceptions = "no-Reply,noreply,nO.reply,"

        EXCEPT = Split(Exceptions, ",")
        For i = LBound(EXCEPT) To UBound(EXCEPT)            

NOREPLY = InStr(1, EM, EXCEPT(i), vbTextCompare)

If NOREPLY > 0 Then
'CbEM.Value = True '~food~
EM = InputBox("NOREPLY E-MAILADRES", "Geef E-mailadres aan", EM)
'else
'CbEM.Value = False ~not food~
End If

        Next i

MsgBox EM

End Sub
希望这能帮助别人