2016-05-30 5 views
3

私の質問は、文字列がこれらのカテゴリに定義されている文字列のいずれかと一致する場合、次の文字列をカテゴリに分類することです。例えば、VBA:一致する複数の文字列

dim test_str as string 

test_str = "tomato" 

、(1)potatoキーワードのいずれかのテストストリングtomato一致する場合(2)tomato及び(3)spaghettiは、次いで、トマト、食品として分類されるであろう。

私は、私が「食べ物」内で定義された10個のキーワードを使用している場合、私はその後、10 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" 

、複数strcompを使用することを含むた、今これを行うのは非常に非効率的な方法を持っています面倒なことだ。これを行うより良い方法はありますか?

答えて

2

のインデックスは、私は単に内のすべての組み合わせを保存したい場合、あなたに

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 

に役立つ機能を書きます文字列をチェックし、値が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 
0

戻り値= -1 ...マッチしない、> 0語

関連する問題