Recherche nom ligne et somme

  • Initiateur de la discussion Initiateur de la discussion Nashou
  • Date de début Date de début

Boostez vos compétences Excel avec notre communauté !

Rejoignez Excel Downloads, le rendez-vous des passionnés où l'entraide fait la force. Apprenez, échangez, progressez – et tout ça gratuitement ! 👉 Inscrivez-vous maintenant !

N

Nashou

Guest
Bonjour,

Je souhaite boucler sur chaque ligne colonne A (sachant que la colonne comporte plusieurs fois la même donnée) et faire la somme des données colonne B associées à chaque ligne correspondante colonne A

J'ai réussi en parti car je fais la boucle mais je ne sais pas faire la somme ....

Private Sub CommandButton1_Click()

Dim x As Integer

For x = 2 To Sheets("TRAITEMENT").Range("A65536").End(xlUp).Row

Set C = Sheets("BASE").Columns(1).Find(Sheets("TRAITEMENT").Range("A" & x), LookIn:=xlValues, lookat:=xlWhole)
If Not C Is Nothing Then
Sheets("BASE").Range("B" & C.Row).Copy Destination:=Sheets("TRAITEMENT").Range("B" & x)
Else
Sheets("TRAITEMENT").Range("B" & x) = "rien"
End If

Next x

End Sub

Ce code ne me donne que la 1ère valeur du nom qu'il trouve ...
Petit fichier ci-joint.

Objectif : faire la somme des nombres colonne B

Merci 🙂
 

Pièces jointes

Re : Recherche nom ligne et somme

Salut Nashou,
voici un exemple par formules.

Est-ce que tu tiens absolument à l'avoir par VBA ?

Si c'est le cas, voici un essai :

VB:
Sub Test()

Dim DL As Integer, Somme As Integer, DL2 As Integer
Dim Nom_Recherche As String

Application.ScreenUpdating = False

Sheets("TRAITEMENT").Select
DL = Cells(65536, 1).End(xlUp).Row

For i = 2 To DL
    Somme = 0
    Nom_Recherche = Cells(i, 1).Value
    Sheets("BASE").Select
    DL2 = Cells(65536, 2).End(xlUp).Row
    For j = 2 To DL2
        If Cells(j, 1).Value = Nom_Recherche Then
            Somme = Somme + Cells(j, 2).Value
        End If
    Next j
    Sheets("TRAITEMENT").Select
    Cells(i, 2).Value = Somme
    
    Nom_Recherche = ""
Next i

End Sub

À mettre dans un module traditionnel.

Vous pouvez appeller la macro comme ceci à partir de votre bouton :
VB:
Private Sub CommandButton1_Click()

Test

End Sub
Cordialement,

Étienne
 

Pièces jointes

Dernière édition:
Re : Recherche nom ligne et somme

Bonsoir à tous
Une proposition :
Code:
[COLOR=DarkSlateGray][B]Private Sub CommandButton1_Click()
Dim i&, j&, dDat(), sDat(), sRng As Range, tmp
  With Sheets("TRAITEMENT")
    Set sRng = .Range(.Cells(1, 1), .Cells(.Rows.Count, 1).End(xlUp).Offset(0, 1))
  End With
  If sRng.Count > 2 Then
    Set sRng = sRng.Offset(1, 0).Resize(sRng.Rows.Count - 1, 2)
    sDat = sRng.Value
    With Sheets("Base")
      dDat = .Range(.Cells(1, 1), .Cells(.Rows.Count, 1).End(xlUp).Offset(0, 1)).Value
    End With
    For i = 1 To UBound(sDat, 1)
      tmp = sDat(i, 1)
      sDat(i, 2) = Empty
      For j = 2 To UBound(dDat, 1)
        If dDat(j, 1) = tmp Then sDat(i, 2) = sDat(i, 2) + dDat(j, 2)
      Next j
    Next i
    sRng.Value = sDat
  End If
End Sub[/B][/COLOR]
ROGER2327
#4633


Dimanche 1er Sable 138 (Noces de Balkis et de Salomon, ST)
11 Frimaire An CCXIX
2010-W48-3T21:31:27Z
 
Re : Recherche nom ligne et somme

Bonsoir à tous
Une proposition :
Code:
[COLOR=DarkSlateGray][B]Private Sub CommandButton1_Click()
Dim i&, j&, dDat(), sDat(), sRng As Range, tmp
  With Sheets("TRAITEMENT")
    Set sRng = .Range(.Cells(1, 1), .Cells(.Rows.Count, 1).End(xlUp).Offset(0, 1))
  End With
  If sRng.Count > 2 Then
    Set sRng = sRng.Offset(1, 0).Resize(sRng.Rows.Count - 1, 2)
    sDat = sRng.Value
    With Sheets("Base")
      dDat = .Range(.Cells(1, 1), .Cells(.Rows.Count, 1).End(xlUp).Offset(0, 1)).Value
    End With
    For i = 1 To UBound(sDat, 1)
      tmp = sDat(i, 1)
      sDat(i, 2) = Empty
      For j = 2 To UBound(dDat, 1)
        If dDat(j, 1) = tmp Then sDat(i, 2) = sDat(i, 2) + dDat(j, 2)
      Next j
    Next i
    sRng.Value = sDat
  End If
End Sub[/B][/COLOR]
ROGER2327
#4633


Dimanche 1er Sable 138 (Noces de Balkis et de Salomon, ST)
11 Frimaire An CCXIX
2010-W48-3T21:31:27Z

Bonsoir !

Je n'ai pas été dispo du week-end pour bosser sur mes macros mais me revoilà 🙂
Merci à toi ROGER, c'est parfaitement ça !!
Je vais décortiquer ton code afin de bien comprendre chaque étape.
Je te remercie 😉
 
- Navigue sans publicité
- Accède à Cléa, notre assistante IA experte Excel... et pas que...
- Profite de fonctionnalités exclusives
Ton soutien permet à Excel Downloads de rester 100% gratuit et de continuer à rassembler les passionnés d'Excel.
Je deviens Supporter XLD
Assurez vous de marquer un message comme solution pour une meilleure transparence.

Discussions similaires

Réponses
7
Affichages
174
Réponses
5
Affichages
307
Réponses
3
Affichages
255
Réponses
2
Affichages
411
Retour