Creer une fonction sommeprod en VBA

Bonjour

Je souhaite creer une fonction en vba afin de simplifier celle ci-dessous:

SOMMEPROD((GAUCHE((INDIRECT($A$1&"!$a$2:$a$1000"));NBCAR(C10))=GAUCHE(C10;11))*((INDIRECT($A$1&"!$c$2:$c$1000"))))

Mon but est de créer une fonction type

Function Resultat(ByVal Feuille As String, Compte As Range, Donnee As Range, Criteria As String) As Double
Dim frml As String

  Dim WS As Worksheet
  Dim a, b, c, Criteria As Range
  Dim plage1, plage2, data3, choix As String
  Dim LasTRow As Long

  Set WS = ThisWorkbook.Sheets(Feuille)
  LasTRow = WS.Cells(Rows.Count, 1).End(xlUp).Row

  Set a = WS.Range(Compte)
  Set b = WS.Range(Donnee)

 plage1 = a.Address
  plage2 = b.Address
  choix = Criteria

frml = SumProduct(((Left(plage1, Len(choix)) = (Left(choix, 11))) * (plage2)))

'=SOMMEPROD((GAUCHE(BALANCE!A1:A175;NBCAR(H20))=GAUCHE(H20;11))*(BALANCE!F1:F175))
Resultat = Evaluate(frml)

le fichier explique le but de la fonction

merci de votre aide

Bonjour,

Une fonction perso mais sans utilisation de SumProduct() :

Function Result(NomFeuille As String, ColonneCode As Integer, ColonneValeur As Integer, Critere As String) As Double

    Dim Fe As Worksheet
    Dim PlgCode As Range
    Dim PlgValeur As Range
    Dim I As Long
    Dim Debut As Long
    Dim Fin As Long
    Dim Total As Double

    Set Fe = ThisWorkbook.Sheets(NomFeuille)

    Debut = Fe.Cells(1, ColonneCode).End(xlDown).Row
    Fin = Fe.Cells(Rows.Count, ColonneCode).End(xlUp).Row
    With Fe: Set PlgCode = .Range(.Cells(Debut, ColonneCode), .Cells(Fin, ColonneCode)): End With

    Debut = Fe.Cells(1, ColonneValeur).End(xlDown).Row
    Fin = Fe.Cells(Rows.Count, ColonneValeur).End(xlUp).Row
    With Fe: Set PlgValeur = .Range(.Cells(Debut, ColonneValeur), .Cells(Fin, ColonneValeur)): End With

    For I = 1 To PlgCode.Count

        If Left(PlgCode(I).Value, Len(Critere)) = Critere Then Total = Total + PlgValeur(I).Value

    Next I

    Result = Total

End Function

que tu appelles depuis Excel : =Result($A$1;1;3;B3)

où 1 et 3 sont les numéros de colonnes pour les plage et B3 le critère

ou en VBA par exemple : MsgBox Result("aa", 1, 3, 104)

Je n'ai voulu être en mode privé mais un suis un nouveau utilisateur du forum.

J'ai testé la fonction mais les valeurs ne remonte pas mais je n'ai pas d'explication.

Je joins le fichier pour analyse

Bonjour,

Essais avec ceci, je suis parti du principe que la plage commence en ligne 1 :

Function Result(NomFeuille As String, ColonneCode As Integer, ColonneValeur As Integer, Critere As String) As Double

    Dim Fe As Worksheet
    Dim PlgCode As Range
    Dim PlgValeur As Range
    Dim I As Long
    Dim Fin As Long
    Dim Total As Double

    Set Fe = ThisWorkbook.Sheets(NomFeuille)

    Fin = Fe.Cells(Rows.Count, ColonneCode).End(xlUp).Row
    With Fe: Set PlgCode = .Range(.Cells(1, ColonneCode), .Cells(Fin, ColonneCode)): End With

    Fin = Fe.Cells(Rows.Count, ColonneValeur).End(xlUp).Row
    With Fe: Set PlgValeur = .Range(.Cells(1, ColonneValeur), .Cells(Fin, ColonneValeur)): End With

    For I = 1 To PlgCode.Count

        If Left(PlgCode(I).Value, Len(Critere)) = Critere Then Total = Total + PlgValeur(I).Value

    Next I

    Result = Total

End Function
Rechercher des sujets similaires à "creer fonction sommeprod vba"