Texte CountCharacter

Magic_Doctor

XLDnaute Barbatruc
Compte le nombre de fois où un caractère (lettre | chiffre) apparaît dans une cellule ou une plage.
VB:
Function CountCharacter(InRange As Range, S As String) As Long
'Compte le nombre de fois où un caractère (lettre | chiffre) apparaît dans une cellule ou une plage
'- InRange : une cellule qui contient une chaîne de caractères
'- S : le caractère que l'on recherche dans la chaîne
'Chip Pearson
    
Dim Rng As Range

  For Each Rng In InRange
    CountCharacter = CountCharacter + Len(Rng.Text) - Len(Application.Substitute(UCase(Rng.Text), UCase(S), ""))
  Next Rng
 
End Function
 

Etoto

XLDnaute Barbatruc
Bonjour,

J'ai une question, quand tu dits que cela compte le nombres de caractères dans une plage, l'intérieur des cellules comptent ? Exemple : Je cherche le numéro 3 et dans ma colonne j'ai :

7
3
243
48
3000
134

Maintenant il va me compter quoi ? 1 parce que y'a qu'un seul 3 dans la colonne ou il va me compter 4 parce que dans la colonne y'a 4 fois le chiffre trois qui apparaît à l'intérieur d'une cellule ?

Merci d'avance
 
Dernière édition:

Etoto

XLDnaute Barbatruc
Bonjour Etoto,
Je viens de faire un essai sur la plage entière et le résultat est 4 (4 fois le 3 dans cette plage)
@+ Lolote83
Ok, dans ce cas il faudrait peut être rajouter un nouveau paramètre à cette fonction du genre :

=CountCharacter(A1:A200;3;1) 1= Recherche Exacte (Compte que les 3)
=CountCharacter(A1:A200;3;2) 2= Recherche approchante (Compte 0,3 343 243 38 73 etc...)

Si quelqu'un est capable d'améliorer la fonction je suis preneur.
 

laurent950

XLDnaute Accro
Bonjour @Etoto,

Une solution VBA (Pattern à compléter le cas échéants)

=CountCharacter(A1:A200;3;1) 1= Recherche Exacte (Compte que les 3)
=CountCharacter(A1:A200;3;2) 2= Recherche approchante (Compte 0,3 343 243 38 73 etc...)

VB:
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
 

Etoto

XLDnaute Barbatruc
Bonjour @Etoto,

Une solution VBA (Pattern à compléter le cas échéants)

VB:
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
Re,

Super top ta fonction, hop dans mon Excel ;) 😜 😁 .
 

Discussions similaires

Statistiques des forums

Discussions
312 112
Messages
2 085 411
Membres
102 885
dernier inscrit
AISSOU