2014-01-09 3 views
-1

VBA에 익숙하지 않고 문자열 배열을 대상 문자열과 일치시키는 함수를 만들려고합니다. 배열의 문자열 중 하나라도 대상 문자열에 포함되어 있으면 true를 반환하고 싶습니다.VBA 일치 함수

나는이 기능이 매우 단순하지만 느린 구문으로 인해 많은 어려움을 겪고 있다고 느낍니다. 어떤 조언이 도움이 될 것입니다!

답변

1
Function matching(ByRef stringList() As Variant, targetString As String) 

    Dim index As Integer 

    For index = 0 To UBound(stringList) 
     If targetString Like "*" & stringList(index) & "*" Then 
      matching = True 
      Exit For 
     Else 
      matching = False 
     End If 
    Next index 

End Function 

사용 :

Public Sub Test() 

    Dim stringList() As Variant 

    stringList = Array("hi", "de", "ho") 

    Debug.Print matching(stringList, "this") 
    Debug.Print matching(stringList, "that") 
    Debug.Print matching(stringList, "hollow") 

End Sub 

출력 :

True 
False 
True 

설명 :처럼 사용

별표 OP와 같이 연산자는 와일드 카드로 작동합니다. 문자열이 대상 문자열에 포함되어 있는지 여부에 관심이 있다고합니다.