Vérifier si une chaîne contient une sous-chaîne en VBA
- 
          
            Utilisation de la fonction Instr()pour vérifier si la chaîne principale contient une sous-chaîne
- 
          
            Utilisation de la fonction InstrRev()pour vérifier si la chaîne principale contient une sous-chaîne
- 
          
            Utilisation de l’opérateur Likepour vérifier si la chaîne principale contient une sous-chaîne
 
Cet article démontrera l’utilisation de la fonction Instr(), de la fonction InstrRev() et de la fonction Like pour vérifier si la chaîne principale contient une sous-chaîne.
Utilisation de la fonction Instr() pour vérifier si la chaîne principale contient une sous-chaîne
    
Syntaxe de la fonction Instr() :
InStr([ start ], string1, string2, [ compare ])
Type de retour : Entier
Paramètres:
| [ start ] | Optionnel. Valeur numérique où la recherche commencera. Pour l’argument [ start ], ci-dessous sont les valeurs correspondantes :1- [Par défaut] La recherche commencera au début de la chaîne principalen- La recherche commencera à la positionn. | 
| string1 | Obligatoire. La chaîne à rechercher (chaîne principale) | 
| string2 | Obligatoire. La chaîne à rechercher. | 
| [ compare ] | Optionnel. Indique quelle méthode de comparaison de chaînes sera utilisée. Pour l’argument [ compare ], ci-dessous figurent les valeurs correspondantes :0- [Par défaut] Méthode de comparaison binaire (sensible à la casse)1- Méthode de comparaison de texte (insensible à la casse) | 
Le bloc de code ci-dessous vérifiera si une sous-chaîne se trouve dans la chaîne principale dans VBA à l’aide de la fonction Instr().
Function IsSubstring(pos as Integer, mainStr as String, subStr as String,compTyp as Integer) as boolean
    'if `Instr()` function returned 0 then the substring is not present in the main string.
    'If `Instr()` function returned a value greater than `0`, would mean that the substring is in the main string.
    If Instr(pos,mainStr,subStr,compTyp) >0 Then
        IsSubstring = true
        Else: IsSubstring = false
    End if
End Function
Sub test1()
    Debug.print IsSubstring(1,"ABCDE","C",1)
End Sub
Sub test2()
    Debug.print IsSubstring(1,"ABCDE","F",1)
End Sub
Sub test3()
    Debug.print IsSubstring(1,"ABCDE","c",0)
End Sub
Sortie test1 :
True
Sortie test2 :
False
Sortie test3 :
False
Le bloc de code ci-dessous renverra la position de la sous-chaîne à partir de la chaîne principale à l’aide de la fonction Instr().
Function GetPosition(pos as Integer, mainStr as String, subStr as String,compTyp as Integer)
    'Check first if the substring is in the main string.
     If InStr(pos, mainStr, subStr, compTyp) > 0 Then
        'if substring is in the main string then get the position of the substring in the main string.
       GetPosition = InStr(1, mainStr, subStr, 1)
       Else: GetPosition = ("Subtring is not in the main string.")
   End If
End Function
Sub test1()
    'Check if `C` is in `ABCDE` starting at the first letter (A), case insensitive.
    Debug.Print GetPosition(1,"ABCDE", "C",1)
End Sub
Sub test2()
    'Check if `c` is in `ABCDE` starting at the first letter of the main string, case sensitive.
    Debug.Print GetPosition(1,"ABCDE", "c",0)
End Sub
Sub test3()
    'Check if `c` is in `ABCDE` starting at the fourth letter of the main string, case sensitive.
    Debug.Print GetPosition(4,"ABCDE", "c",0)
End Sub
Sortie test1 :
3
Sortie test2 :
Subtring is not in the main string.
Sortie test3 :
Subtring is not in the main string.
Utilisation de la fonction InstrRev() pour vérifier si la chaîne principale contient une sous-chaîne
Syntaxe de la fonction InstrRev() :
InStrRev(string1, string2,[ start ], [ compare ])
Type de retour : Entier
Paramètres:
| string1 | Obligatoire. La chaîne à rechercher (chaîne principale) | 
| string2 | Obligatoire. La chaîne à rechercher. | 
Le bloc de code ci-dessous vérifiera si une sous-chaîne se trouve dans la chaîne principale dans VBA à l’aide de la fonction InstrRev().
Function IsSubstring(mainStr As String, subStr As String) As Boolean
    'if `InstrRev()` function returned 0 then the substring is not present in the main string.
    'If `InstrRev()` function returned a value greater than `0`, would mean that the substring is in the main string.
    If InStrRev(mainStr, subStr) > 0 Then
        IsSubstring = True
        Else: IsSubstring = False
    End If
End Function
Sub test1()
    Debug.Print IsSubstring("ABCDE", "C")
End Sub
Sub test2()
    Debug.Print IsSubstring("ABCDE", "F")
End Sub
Sortie test1 :
True
Sortie test2 :
False
Utilisation de l’opérateur Like pour vérifier si la chaîne principale contient une sous-chaîne
Syntaxe de l’opérateur Like :
res = string Like pattern
Type de retour : booléen
Paramètres:
| res | Obligatoire. Valeur de retour en booléen | 
| string | Obligatoire. La chaîne à examiner | 
| pattern | Obligatoire. Chaîne à rechercher. Voir Remarquespour plus de détails | 
Remarques:
| ? | N’importe quel caractère | 
| * | N’importe quel caractère de 0 à plusieurs | 
| # | N’importe quel numéro (0 à 9) | 
Le bloc de code ci-dessous vérifiera si une sous-chaîne se trouve dans la chaîne principale dans VBA à l’aide de l’opérateur Like.
Function IsSubString(mainStr as String,subStr as String) as Boolean
    'Check if subStr is in the main string by using *
   If mainStr Like "*" & subStr & "*" Then
        IsSubString = True
        Else: IsSubstring= False
   End If
End Function
Sub test1()
    Debug.print (IsSubString("ABCDE","C"))
End Sub
Sub test2()
    Debug.print (IsSubString("ABCDE","c"))
End Sub
Sub test3()
    Debug.print (IsSubString("ABCDE","F"))
End Sub
Sortie test1 :
True
Sortie test2 :
False
Sortie test3 :
False