Option Explicit
Function CountCharacter(InRange As Range, S As String, N As Byte) As Long
'' https://eurelis.com/blog/expressions-regulieres
' Code OK
Dim Matches As Object
Dim Match As Object: Dim StrPattern As String ' La cible caractéres rechecher par Pattern !
Dim reg As Object
Set reg = CreateObject("VBScript.RegExp")
Dim CherchePattern(1 To 2) As Variant
' Stock Pattern.
CherchePattern(1) = "(^" & S & "\b\s)|(^" & S & "\b)|(\s" & S & "\b\s)|(\s" & S & "\b$)" ' 1 = Recherche Exacte
CherchePattern(2) = "([" & S & "])" ' 2 = Recherche approchante
Dim i As Integer
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim Rng As Range
For Each Rng In InRange
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Recherche en Ligne :
' ---------------------
reg.Pattern = CherchePattern(N)
reg.MultiLine = True: reg.IgnoreCase = False: reg.Global = True: Debug.Print reg.test(Rng.Text)
Set Matches = reg.Execute(Rng.Text)
' Cible de la recherche (Partie du Mots ou chaine recherché !)
' For Each Match In Matches
' Debug.Print "source >>", Match.Value
' For i = 0 To Match.SubMatches.Count - 1
' Debug.Print "[$" & i + 1 & "]", Match.SubMatches(i)
' Next i
' Next Match
CountCharacter = CountCharacter + Matches.Count
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Next
' libération d'objets
Set Matches = Nothing
Set Match = Nothing
Set reg = Nothing
End Function