VBA/Excel/Access/Word/String Functions/Like

Материал из VB Эксперт
Перейти к: навигация, поиск

Use Like to compare strings

 
Sub CompareStrings()
    Dim strString1 As String
    Dim strString2 As String
    
    strString1 = "Microsoft"
    
    If strString1 Like "Micr*" Then
    Debug.Print "True"
    End If
    
    If strString1 Like "Mic*t" Then
    Debug.Print "True"
    End If
    
End Sub



Wild Cards Used to Build Patterns with Like

 
Character      Matches         
*              Zero or more characters        
?              Any single character        
#              Any single digit (0-9)        
[list]         Any single character in the specified list        
[!list]        Any single character not in the specified list      

Sub str()
    Dim str1 As String
    str1 = "Exit"
    result = (str1 Like "E*")     "result holds False
    result = (str2 Like "E*")     "result holds True
    result = (str2 Like "?x?*")     "result holds True
    result = (str1 Like "##")     "result holds True
    result = (str2 Like "[E,e]*")     "result holds True
End Sub