Passer le résultat d'une procédure à une variable
Bonjour à tous,
Après avoir écrit une petite procédure me retournant une chaîne de caractères spécifique, j'aimerais attribuer cette valeur à une variable.
Comme je suis une pomme en VBA car tout débutant, comment le faire?
Voici la procédure qui fonctionne et me retourne bien la chaîne souhaitée.
Sub SynchrFind()
Dim Cel As Range, I As Integer, NbG6 As Long, NbG7 As Long, NBExcl As Long, RowNb As Long, Synchr As String
RowNb = 1000
For Each Cel In Range("D1:F" & RowNb)
If Cel.Value Like "G6#0*" Then
NbG6 = NbG6 + 1
ElseIf Cel.Value Like "G7#0*" Then
NbG7 = NbG7 + 1
ElseIf Cel.Value Like "N*!" Then
NBExcl = NBExcl + 1
End If
Next Cel
If NbG6 > NbG7 And NbG6 > NBExcl Then
Synchr = "G6#0*"
ElseIf NbG7 > NbG6 And NbG7 > NBExcl Then
Synchr = "G7#0*"
ElseIf NBExcl > NbG6 And NBExcl > NbG7 Then
Synchr = "N*!"
End If
End SubJ'aimerais maintenant attribuer cette valuer à une variable par
Synchar = SynchrFInd ' il manque quelque chose probablement car la variable reste vide
Merci par avance et cordiales salutations
André
Invité
Bonjour André,
Function SynchrFind()
Dim Cel As Range, I As Integer, NbG6 As Long, NbG7 As Long, NBExcl As Long, RowNb As Long, Synchr As String
RowNb = 1000
For Each Cel In Range("D1:F" & RowNb)
If Cel.Value Like "G6#0*" Then
NbG6 = NbG6 + 1
ElseIf Cel.Value Like "G7#0*" Then
NbG7 = NbG7 + 1
ElseIf Cel.Value Like "N*!" Then
NBExcl = NBExcl + 1
End If
Next Cel
If NbG6 > NbG7 And NbG6 > NBExcl Then
Synchr = "G6#0*"
ElseIf NbG7 > NbG6 And NbG7 > NBExcl Then
Synchr = "G7#0*"
ElseIf NBExcl > NbG6 And NBExcl > NbG7 Then
Synchr = "N*!"
End If
' Retour de la valeur
SynchrFind = Synchr
End FunctionIl faut transformer la Procédure en Fonction et vous pourrez ainsi l'affecter à votre variable
A+
Bonjour Bruno,
Merci beaucoup, ça c'est d'la musique!!!! ça fonctionne parfaitement.
Encore merci et bonne soirée
Cordiales salutations
André