Besoin d'explications " For each ...... "

KyFranRD

XLDnaute Occasionnel
Bien le bonjour à tous

Si possible je désirerai avoir des explications sur un code en particulier:

Code placé sur Feuille:


Private Sub CommandButton1_Click()


' Commentaires

On Error Resume Next
For Each cel In Range("B1:B" & Range("B5000").End(xlUp).Row)
Attente.Add CStr(cel.Value), CStr(cel.Value)
Next
On Error GoTo 0

'affichage

For i = 1 To Attente.Count - 1
Cells(i + 1, 15) = Attente(i + 1)
Cells(i + 1, 16).FormulaR1C1 = "=conca(RC[-1])"
Next
Set Attente = Nothing

' UO Concernées

On Error Resume Next
For Each cel In Range("B1:B" & Range("B5000").End(xlUp).Row)
Attente.Add CStr(cel.Value), CStr(cel.Value)
Next
On Error GoTo 0

'affichage

For j = 1 To Attente.Count - 1
Cells(j + 1, 15) = Attente(j + 1)
Cells(j + 1, 17).FormulaR1C1 = "=conca2(RC[-2])"
Next
Set Attente = Nothing


End Sub


Code placé sur module

Function conca(titre As String)

Application.Volatile

For Each cel In Range("b2:b" & Range("b5000").End(xlUp).Row)

If cel = titre And InStr(conca, Cells(cel.Row, "I")) = 0 Then

conca = conca & Cells(cel.Row, "I") & " ; "

End If

Next

conca = Left(conca, Len(conca) - 2)

End Function

Function conca2(titre As String)

Application.Volatile

For Each cel In Range("b2:b" & Range("b5000").End(xlUp).Row)

If cel = titre And InStr(conca2, Cells(cel.Row, "H")) = 0 Then

conca2 = conca2 & Cells(cel.Row, "H") & " ; "

End If

Next

conca2 = Left(conca2, Len(conca2) - 2)

End Function



Je suis assez, pas mal, beaucoup novice en langage VBA. Je me suis donc fais aider pour réaliser ce code qui fonctionne trés bien. Il me permet sur une Feuille1 de regrouper sans doublons une série d'information m’étant trés utile. Par contre:

Quelqu'un pourrait-il m'expliquer ce code? De façons "car je galère" à pouvoir l'appliquer sur une feuille2. Je ne parviens pas à le faire.

Faut-il recréer 2 fonctions " conca 3 et 4 "? Dsl si j'en demande beaucoup ...

Voilà le code que j'ai tenté de modifier pour application sur feuille2

sur feuille2

Private Sub CommandButton1_Click()

' Commentaires

On Error Resume Next
For Each cel In Range("B1:B" & Range("B5000").End(xlUp).Row)
Attente.Add CStr(cel.Value), CStr(cel.Value)
Next
On Error GoTo 0

'affichage
For i = 1 To Attente.Count - 1
Cells(i + 1, 15) = Attente(i + 1)
Cells(i + 1, 16).FormulaR1C1 = "=conca3(RC[-1])"
Next
Set Attente = Nothing

' UO Concernées

On Error Resume Next
For Each cel In Range("B1:B" & Range("B5000").End(xlUp).Row)
Attente.Add CStr(cel.Value), CStr(cel.Value)
Next
On Error GoTo 0

'affichage
For j = 1 To Attente.Count - 1
Cells(j + 1, 15) = Attente(j + 1)
Cells(j + 1, 17).FormulaR1C1 = "=conca4(RC[-2])"
Next
Set Attente = Nothing

end sub

et sur module

Function conca3(titre As String)

Application.Volatile

For Each cel In Range("b2:b" & Range("b5000").End(xlUp).Row)

If cel = titre And InStr(conca3, Cells(cel.Row, "I")) = 0 Then

conca3 = conca3 & Cells(cel.Row, "I") & " ; "

End If

Next

conca3 = Left(conca3, Len(conca3) - 2)

End Function

Function conca4(titre As String)

Application.Volatile

For Each cel In Range("b2:b" & Range("b5000").End(xlUp).Row)

If cel = titre And InStr(conca4, Cells(cel.Row, "H")) = 0 Then

conca4 = conca4 & Cells(cel.Row, "H") & " ; "

End If

Next

conca4 = Left(conca4, Len(conca4) - 2)

End Function


Merci à l'avance pour toute personne me venant en aide

Petit détail important ou pas. Les deux boutons ne sont pas les mêmes. Chaque feuille à son bouton propre
 

Pièces jointes

  • Test.xls
    245.5 KB · Affichages: 54
  • Test.xls
    245.5 KB · Affichages: 59
  • Test.xls
    245.5 KB · Affichages: 55
Dernière édition:

camarchepas

XLDnaute Barbatruc
Re : Besoin d'explications " For each ...... "

Bonjour,

procédons par petit morceaux :

' Commentaires

On Error Resume Next
For Each cel In Range("B1:B" & Range("B5000").End(xlUp).Row)
Attente.Add CStr(cel.Value), CStr(cel.Value)
Next
On Error GoTo 0

On Error Resume Next
1° remarque : JE sais qu'il y a des adeptes , mais le on error resume next est à utiliser avec parcimonie et de manière très cadrée car lorsqu'une erreur ce produit, le programme passe à la ligne suivante et ceci sans alarme ou message, donc l'on peut livrer des résultats faux sans s'en rentre compte.

For Each cel In Range("B1:B" & Range("B5000").End(xlUp).Row)

on décompose :
Range("B5000").End(xlUp).Row

Recherche de la premiere ligne non nulle en partant de la cellule B5000 et en remontant vers B1

For Each cel In Range("B1:B" & Range("B5000").End(xlUp).Row)

Parcours donc l'ensemble des cellules de la colonne B ligne1 à la derniere ligne non null ligne


Si tu à d'autre questions plus ciblées n'hésites pas.
 

KyFranRD

XLDnaute Occasionnel
Re : Besoin d'explications " For each ...... "

Merci bien camarchepas pour ces informations ..... ;)


Quelqu'un pourrait-il m'indiquer d'où vient mon erreur?
Pourquoi, lorsque je clique sur le bouton feuiile1 cela agit sur la feuille 2 en me créant un défaut?
Même problème pour la feuille2. Voir fichier joint



Merci à l'avance
 
Dernière édition:

Discussions similaires

Statistiques des forums

Discussions
312 177
Messages
2 085 972
Membres
103 073
dernier inscrit
MSCHOE16