Bonjour à tous,
Avec les expressions régulières ...
Voici la fonction avec une expression régulière minimaliste.
Function CheckString(ByVal Value As String, Optional ByVal Pattern As String) As Boolean
Dim localPattern As String
localPattern = Pattern
If localPattern = vbNullString Then localPattern = "^[a-zA-Z]{3}.*\d{4}$"
Dim regex As Object
Set regex = CreateObject("VBScript.RegExp")
regex.Pattern = localPattern
regex.IgnoreCase = True
regex.Global = False
Dim Result As Boolean
Result = regex.test(Value)
CheckString = Result
End Function
Et pour le test :
Sub testCheckString()
Dim Ele As Variant
For Each Ele In VBA.Array("Ab12", "ABCD12", "AbcD1234", "12AB1234")
Dim strTemp As String
strTemp = strTemp & Ele & " " & "Résultat : " & CheckString(Ele) & vbNewLine
Next Ele
MsgBox strTemp
End Sub
Bonne programation.