Sommeprod en vba

biloute91

XLDnaute Occasionnel
Bonjour le forum,

la question est dans le titre. Je cherche l'équivalent de cette fonction en vba.
Je souhaite faire des statistiques à partir d'un tableau qui comporte environ 20 colonnes et un nombre de lignes indeterminées, et ce à partir d'un formulaire (non encore conçu).
Par exemple :
critère 1ere colonne = fruit (pomme,banane,poire)
critère 2ème colonne = couleur (rouge,jaune,verte,bleue)
critère 3ème colonne = qualité du fruit (mûr,très mur...)

Je souhaite savoir (par exemple)le nombre de pomme rouge mûr, soit en selectionnant le fruit dans une combobox soit à partir d'une Textbox

J'espere être clair.
Merci pour votre aide
 

biloute91

XLDnaute Occasionnel
Re : Sommeprod en vba

Ci-joint un fichier retravailler pour retirer les donnees confidentielles.
L'idée est de pouvoir faire ,si possible, des statistiques selon différents critères.
 

Pièces jointes

  • test3.zip
    40 KB · Affichages: 62
  • test3.zip
    40 KB · Affichages: 50
  • test3.zip
    40 KB · Affichages: 59

cbea

XLDnaute Impliqué
Re : Sommeprod en vba

Bonsoir Biloute91, pierrejean,

N'ayant pas compris le contenu de ton fichier, je te transmets un exemple dans le fichier joint.
En cellules F3, G3 et H3, tu mets les critères que tu veux (tu n'es pas obligé de saisir tous les critères) et ensuite, tu cliques sur le bouton "Calculer" pour avoir le nombre d'éléments correspondant aux critères.
 

Pièces jointes

  • biloute91_v1.xls
    30.5 KB · Affichages: 103

biloute91

XLDnaute Occasionnel
Re : Sommeprod en vba

Bonjour au forum , bonjour cbea

J'ai adapté ton code à mes besoins et cela fonctionne parfaitement. Toutefois j'ai voulu ajouté un critère de recherche supplémentaire mais cette fois-ci numérique et non alpha. Mon problème est que le résultat ressort systématiquement à 0.

Code:
Dim strSumproduct As String
    
    
    If TextBox2.Value <> "" Then
        strSumproduct = "((LPeri = """ & TextBox2.Value & """)*1)"
    End If
    
    
    If TextBox3.Value <> "" Then
        If strSumproduct <> "" Then strSumproduct = strSumproduct & "*"
        strSumproduct = strSumproduct & "((LObjet = """ & TextBox3.Value & """)*1 )"
    End If
    
    
    If ComboBox2.Value <> "" Then
        If strSumproduct <> "" Then strSumproduct = strSumproduct & "*"
        strSumproduct = strSumproduct & "((LTransmis = """ & ComboBox2.Value & """)*1)"
    End If
      If ComboBox6.Value <> "" Then
        If strSumproduct <> "" Then strSumproduct = strSumproduct & "*"
        strSumproduct = strSumproduct & "((LMeco = """ & ComboBox6.Value & """)*1)"
    End If
      If ComboBox7.Value <> "" Then
        If strSumproduct <> "" Then strSumproduct = strSumproduct & "*"
        strSumproduct = strSumproduct & "((LResp = """ & ComboBox7.Value & """)*1)"
    End If
      If ComboBox8.Value <> "" Then
        If strSumproduct <> "" Then strSumproduct = strSumproduct & "*"
        strSumproduct = strSumproduct & "((LPTF = """ & ComboBox8.Value & """)*1)"
    End If
    
    If TextBox6.Value <> "" Then
        strSumproduct = "((LPeri = """ & TextBox6.Value & """)*1)"
    End If
    
    
    If TextBox7.Value <> "" Then
        If strSumproduct <> "" Then strSumproduct = strSumproduct & "*"
        strSumproduct = strSumproduct & "((LObjet = """ & TextBox7.Value & """)*1 )"
    End If
    
    
    If ComboBox12.Value <> "" Then
        If strSumproduct <> "" Then strSumproduct = strSumproduct & "*"
        strSumproduct = strSumproduct & "((LTransmis = """ & ComboBox12.Value & """)*1)"
    End If
      If ComboBox10.Value <> "" Then
        If strSumproduct <> "" Then strSumproduct = strSumproduct & "*"
        strSumproduct = strSumproduct & "((LMeco = """ & ComboBox10.Value & """)*1)"
    End If
      If ComboBox11.Value <> "" Then
        If strSumproduct <> "" Then strSumproduct = strSumproduct & "*"
        strSumproduct = strSumproduct & "((LResp = """ & ComboBox11.Value & """)*1)"
    End If
      If ComboBox9.Value <> "" Then
        If strSumproduct <> "" Then strSumproduct = strSumproduct & "*"
        strSumproduct = strSumproduct & "((LPTF = """ & ComboBox9.Value & """)*1)"
    End If
      If TextBox8.Value <> "" Then
        strSumproduct = "((LUID = """ & TextBox8.Value & """)*1)"
    End If
    
    
    [COLOR="Red"]If TextBox9.Value <> "" Then
        If strSumproduct <> "" Then strSumproduct = strSumproduct & "*"
        strSumproduct = strSumproduct & "((LUID = """ & TextBox9.Value & """)*1 )"
    End If
    If TextBox10.Value <> "" Then
        If strSumproduct <> "" Then strSumproduct = strSumproduct & "*"
        strSumproduct = strSumproduct & "((Ldelai = """ & TextBox10.Value & """)*1 )"
    End If[/COLOR]
   
   
    ' Calculer le nombre d'éléments en utilisant Evaluate
    TextBox5.Value = 0
    If strSumproduct <> "" Then
        TextBox5.Value = Evaluate("SUMPRODUCT(" & strSumproduct & ")")
    End If
End Sub


Les deux lignes en rouge concernent les critères numériques de la recherche et servant au calcul.

Merci pour votre aide
 

cbea

XLDnaute Impliqué
Re : Sommeprod en vba

Bonjour biloute91 et à tous,

Voici une solution à tester :
Remplacer :
Code:
    If TextBox9.Value <> "" Then
        If strSumproduct <> "" Then strSumproduct = strSumproduct & "*"
        strSumproduct = strSumproduct & "((LUID = """ & TextBox9.Value & """)*1 )"
    End If
    If TextBox10.Value <> "" Then
        If strSumproduct <> "" Then strSumproduct = strSumproduct & "*"
        strSumproduct = strSumproduct & "((Ldelai = """ & TextBox10.Value & """)*1 )"
    End If
par :
Code:
    If TextBox9.Value <> "" Then
        If strSumproduct <> "" Then strSumproduct = strSumproduct & "*"
        strSumproduct = strSumproduct & "((LUID = [COLOR="Red"]"[/COLOR] & TextBox9.Value & [COLOR="red"]"[/COLOR])*1 )"
    End If
    If TextBox10.Value <> "" Then
        If strSumproduct <> "" Then strSumproduct = strSumproduct & "*"
        strSumproduct = strSumproduct & "((Ldelai = [COLOR="red"]"[/COLOR] & TextBox10.Value & [COLOR="red"]"[/COLOR])*1 )"
    End If

Si cela ne fonctionne pas, il sera préférable de joindre un extrait du fichier.
 

cbea

XLDnaute Impliqué
Re : Sommeprod en vba

Bonjour biloute91,

Voici une solution.

Il manquait la plage de cellules : LDelai
et j'ai mis la référence : =DECALER(RECENSEMENT!$M$2;;;NBVAL(RECENSEMENT!$A:$A)-1)

De plus, j'ai modifié toutes les plages de cellules nommées pour avoir la même hauteur en mettant : NBVAL(RECENSEMENT!$A:$A)

Ex :
pour la plage LMeco, j'ai remplacé :
=DECALER(RECENSEMENT!$J$2;;;NBVAL(RECENSEMENT!$J:$J)-1)
par
=DECALER(RECENSEMENT!$J$2;;;NBVAL(RECENSEMENT!$A:$A)-1)
 

Pièces jointes

  • test4_v1.zip
    46.4 KB · Affichages: 76

biloute91

XLDnaute Occasionnel
Re : Sommeprod en vba

Merci cbea , cela fonctionne.
Si je souhaite faire la même chose mais avec un format date (via DTPicker) est-ce identique ?
Comme :

Code:
If DTPicker3.Value <> "" Then
        If strSumproduct <> "" Then strSumproduct = strSumproduct & "*"
        strSumproduct = strSumproduct & "((Ldate =  " & DTPicker3.Value & " )*1 )"
    End If
 

cbea

XLDnaute Impliqué
Re : Sommeprod en vba

Tu peux mettre ceci :
Code:
If DTPicker3.Value <> "" Then
        If strSumproduct <> "" Then strSumproduct = strSumproduct & "*"
        strSumproduct = strSumproduct & "((Ldate =  " & CLng(CDate(DTPicker3.Value)) & " )*1 )"
End If
 

biloute91

XLDnaute Occasionnel
Re : Sommeprod en vba

Je suis allé un peu vite.( cela ne retire en rien ce qui j'ai dis précédemment)
En fait lorsque j'entre un critère avec la date cela fonctionne mais lorsque je fais une nouvelle recherche juste après avec un autre critère, le résultat est systématiquement 0.
 

Discussions similaires

Réponses
6
Affichages
1 K

Statistiques des forums

Discussions
312 613
Messages
2 090 231
Membres
104 454
dernier inscrit
alaindeloin.1976