Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

Connaître le nombre d'occurence dans un module

2 réponses
Avatar
alain Lebayle
Bonjour,
Dans un msgbox, je souhaite connaître le nombre d'une occurrence dans un
module. Par exemple le nombre de fois qu'apparaît le mo "Alpha" dans le
module "Test"
Je vous remercie
Alain

--

2 réponses

Avatar
FS
Toujours pour Excel 97, je suppose (sinon, c'est plus simple avec la
fonction Split intégrée) ?
Mais, donc, testé avec Excel 97 :

'======================== Sub NbOccur()
Dim S As String
With ThisWorkbook.VBProject.VBComponents("Test").CodeModule
S = .Lines(1, .CountOfLines)
End With
MsgBox SplitFor97("Alpha", S).Count - 1
End Sub

Function SplitFor97(split_string As String, orig_string As String) As
Collection

If split_string = "" Then Exit Function
Dim Coll As New Collection

Dim pos
pos = 1
Dim token As String
Dim my_string As String
my_string = orig_string

While my_string <> "" And pos <> 0
pos = InStr(1, my_string, split_string)
If pos > 0 Then
token = Left(my_string, pos - 1)
If token <> "" Then Coll.Add token
my_string = Mid(my_string, pos + Len(split_string))
End If
Wend
If my_string <> "" Then Coll.Add my_string
Set SplitFor97 = Coll
End Function
'========================
FS
--
Frédéric SIGONNEAU
Modules et modèles pour Excel :
http://frederic.sigonneau.free.fr/

alain Lebayle a écrit :
Bonjour,
Dans un msgbox, je souhaite connaître le nombre d'une occurrence dans un
module. Par exemple le nombre de fois qu'apparaît le mo "Alpha" dans le
module "Test"
Je vous remercie
Alain



Avatar
alain Lebayle
Encore une fois un très très grand merci...
C'est parfait !
Alain

"FS" a écrit dans le message de news:
eMnBVS%
Toujours pour Excel 97, je suppose (sinon, c'est plus simple avec la
fonction Split intégrée) ?
Mais, donc, testé avec Excel 97 :

'======================== > Sub NbOccur()
Dim S As String
With ThisWorkbook.VBProject.VBComponents("Test").CodeModule
S = .Lines(1, .CountOfLines)
End With
MsgBox SplitFor97("Alpha", S).Count - 1
End Sub

Function SplitFor97(split_string As String, orig_string As String) As
Collection

If split_string = "" Then Exit Function
Dim Coll As New Collection

Dim pos
pos = 1
Dim token As String
Dim my_string As String
my_string = orig_string

While my_string <> "" And pos <> 0
pos = InStr(1, my_string, split_string)
If pos > 0 Then
token = Left(my_string, pos - 1)
If token <> "" Then Coll.Add token
my_string = Mid(my_string, pos + Len(split_string))
End If
Wend
If my_string <> "" Then Coll.Add my_string
Set SplitFor97 = Coll
End Function
'======================== >
FS
--
Frédéric SIGONNEAU
Modules et modèles pour Excel :
http://frederic.sigonneau.free.fr/

alain Lebayle a écrit :
Bonjour,
Dans un msgbox, je souhaite connaître le nombre d'une occurrence dans un
module. Par exemple le nombre de fois qu'apparaît le mo "Alpha" dans le
module "Test"
Je vous remercie
Alain