VBA comment décomposer une fonction car elle dépasse la capacité VBA

Bonjour,

J'ai une fonction qui est tellement longue qu'elle dépasse la capacité de VBA.

Pourriez-vous m'aider s'il vous plait à la décomposer ou à la raccourci pour que tout tienne ?

Sub AppliquerFormuleMontantPrimesAttribuees()

    'Déclarer la constante de la notion de dernière ligne
    Dim DerniereLigne As Long
    'Déclarer la constante de la notion de formule pour déterminer le Montant de la Primes Attribuees
    Dim FormuleMontantPrimesAttribuees As String

    'déterminer la dernière ligne avec des données dans la colonne C
    DerniereLigne = ThisWorkbook.Worksheets("PRIMES EXPERTISE").Range("A" & Rows.Count).End(xlUp).Row

    'Définir la formule pour déterminer le Montant de la Primes Attribuees
    FormuleMontantPrimesAttribuees = "=IFS(AND([@SERVICE]=""ABA"",[@[NOMBRE DE POINTS]]>='BDD Montant'!A3),'BDD Montant'!B3,AND([@SERVICE]=""ABA"",[@[NOMBRE DE POINTS]]>='BDD Montant'!A4),'BDD Montant'!B4,AND([@SERVICE]=""ABA"",[@[NOMBRE DE POINTS]]>='BDD Montant'!A5),'BDD Montant'!B5,AND([@SERVICE]=""ABA"",[@[NOMBRE DE POINTS]]>='BDD Montant'!A6),'BDD Montant'!B6,AND([@SERVICE]=""ABA"",[@[NOMBRE DE POINTS]]>=0),0,AND([@SERVICE]=""DEC"",[@[NOMBRE DE POINTS]]>='BDD Montant'!C3),'BDD Montant'!D3,AND([@SERVICE]=""DEC"",[@[NOMBRE DE POINTS]]>='BDD Montant'!C4),'BDD Montant'!D4,AND([@SERVICE]=""DEC"",[@[NOMBRE DE POINTS]]>='BDD Montant'!C5),'BDD Montant'!D5,AND([@SERVICE]=""DEC"",[@[NOMBRE DE POINTS]]>='BDD Montant'!C6),'BDD Montant'!D6,AND([@SERVICE]=""DEC"",[@[NOMBRE DE POINTS]]>=0),0,AND([@SERVICE]=""EXP"",[@[NOMBRE DE POINTS]]>='BDD Montant'!E3),'BDD Montant'!F3,AND([@SERVICE]=""EXP"",[@[NOMBRE DE POINTS]]>='BDD Montant'!E4),'BDD Montant'!F4,AND([@SERVICE]=""EXP"",[@[NOMBRE DE POINTS]]>='BDD Montant'!E5),'BDD Montant'!F5, AND([@SERVICE]=""EXP"",[@[NOMBRE DE POINTS]]>='BDD Montant'!E6),'BDD Montant'!F6, AND([@SERVICE]=""EXP"",[@[NOMBRE DE POINTS]]>=0),0,AND([@SERVICE]=""ADV"",[@[NOMBRE DE POINTS]]>='BDD Montant'!G3),'BDD Montant'!H3,AND([@SERVICE]=""ADV"",[@[NOMBRE DE POINTS]]>='BDD Montant'!G4),'BDD Montant'!H4,AND([@SERVICE]=""ADV"",[@[NOMBRE DE POINTS]]>='BDD Montant'!G5),'BDD Montant'!H5,AND([@SERVICE]=""ADV"",[@[NOMBRE DE POINTS]]>='BDD Montant'!G6),'BDD Montant'!H6,AND([@SERVICE]=""ADV"",[@[NOMBRE DE POINTS]]>=0),0,AND([@SERVICE]=""EMB"",[@[NOMBRE DE POINTS]]>='BDD Montant'!I3),'BDD Montant'!J3,AND([@SERVICE]=""EMB"",[@[NOMBRE DE POINTS]]>='BDD Montant'!I4),'BDD Montant'!J4,AND([@SERVICE]=""EMB"",[@[NOMBRE DE POINTS]]>='BDD Montant'!I5),'BDD Montant'!J5,AND([@SERVICE]=""EMB"",[@[NOMBRE DE POINTS]]>='BDD Montant'!I6),'BDD Montant'!J6,AND([@SERVICE]=""EMB"",[@[NOMBRE DE POINTS]]>=0),0,AND([@SERVICE]=""MAI"",[@[NOMBRE DE POINTS]]>='BDD Montant'!K3),'BDD Montant'!L3,AND([@SERVICE]=""MAI"",[@[NOMBRE DE POINTS]]>='BDD Montant'!K4),'BDD Montant'!L4,AND([@SERVICE]=""MAI"",[@[NOMBRE DE POINTS]]>='BDD Montant'!K5),'BDD Montant'!L5,AND([@SERVICE]=""MAI"",[@[NOMBRE DE POINTS]]>='BDD Montant'!K6),'BDD Montant'!L6,AND([@SERVICE]=""MAI"",[@[NOMBRE DE POINTS]]>=0),0,AND([@SERVICE]=""NET"",[@[NOMBRE DE POINTS]]>='BDD Montant'!M3),'BDD Montant'!N3,AND([@SERVICE]=""NET"",[@[NOMBRE DE POINTS]]>='BDD Montant'!M4),'BDD Montant'!N4,AND([@SERVICE]=""NET"",[@[NOMBRE DE POINTS]]>='BDD Montant'!M5),'BDD Montant'!N5,AND([@SERVICE]=""NET"",[@[NOMBRE DE POINTS]]>='BDD Montant'!M6),'BDD Montant'!N6,AND([@SERVICE]=""NET"",[@[NOMBRE DE POINTS]]>=0),0)"

    'Appliquer la formule pour déterminer le Montant de la Primes Attribuees depuis la cellule F6 à la dernière ligne
    ThisWorkbook.Worksheets("PRIMES EXPERTISE").Range("F6:F" & DerniereLigne).Formula = FormuleMontantPrimesAttribuees

End Sub

Ces conditions sont par thème d'attribut : ABA, DEC, EXP, ADV, EMB, MAI, NET. Les conditions sont les mêmes pour ces 5 attributs.

ABA :

AND([@SERVICE]=""ABA"",[@[NOMBRE DE POINTS]]>='BDD Montant'!A3),'BDD Montant'!B3,AND([@SERVICE]=""ABA"",[@[NOMBRE DE POINTS]]>='BDD Montant'!A4),'BDD Montant'!B4,AND([@SERVICE]=""ABA"",[@[NOMBRE DE POINTS]]>='BDD Montant'!A5),'BDD Montant'!B5,AND([@SERVICE]=""ABA"",[@[NOMBRE DE POINTS]]>='BDD Montant'!A6),'BDD Montant'!B6,AND([@SERVICE]=""ABA"",[@[NOMBRE DE POINTS]]>=0),0

DEC :

AND([@SERVICE]=""DEC"",[@[NOMBRE DE POINTS]]>='BDD Montant'!C3),'BDD Montant'!D3,AND([@SERVICE]=""DEC"",[@[NOMBRE DE POINTS]]>='BDD Montant'!C4),'BDD Montant'!D4,AND([@SERVICE]=""DEC"",[@[NOMBRE DE POINTS]]>='BDD Montant'!C5),'BDD Montant'!D5,AND([@SERVICE]=""DEC"",[@[NOMBRE DE POINTS]]>='BDD Montant'!C6),'BDD Montant'!D6,AND([@SERVICE]=""DEC"",[@[NOMBRE DE POINTS]]>=0),0

EXP :

AND([@SERVICE]=""EXP"",[@[NOMBRE DE POINTS]]>='BDD Montant'!E3),'BDD Montant'!F3,AND([@SERVICE]=""EXP"",[@[NOMBRE DE POINTS]]>='BDD Montant'!E4),'BDD Montant'!F4,AND([@SERVICE]=""EXP"",[@[NOMBRE DE POINTS]]>='BDD Montant'!E5),'BDD Montant'!F5, AND([@SERVICE]=""EXP"",[@[NOMBRE DE POINTS]]>='BDD Montant'!E6),'BDD Montant'!F6,0, AND([@SERVICE]=""EXP"",[@[NOMBRE DE POINTS]]>=0),0

ADV :

AND([@SERVICE]=""ADV"",[@[NOMBRE DE POINTS]]>='BDD Montant'!G3),'BDD Montant'!H3,AND([@SERVICE]=""ADV"",[@[NOMBRE DE POINTS]]>='BDD Montant'!G4),'BDD Montant'!H4,AND([@SERVICE]=""ADV"",[@[NOMBRE DE POINTS]]>='BDD Montant'!G5),'BDD Montant'!H5,AND([@SERVICE]=""ADV"",[@[NOMBRE DE POINTS]]>='BDD Montant'!G6),'BDD Montant'!H6,AND([@SERVICE]=""ADV"",[@[NOMBRE DE POINTS]]>=0),0

EMB :

AND([@SERVICE]=""EMB"",[@[NOMBRE DE POINTS]]>='BDD Montant'!I3),'BDD Montant'!J3,AND([@SERVICE]=""EMB"",[@[NOMBRE DE POINTS]]>='BDD Montant'!I4),'BDD Montant'!J4,AND([@SERVICE]=""EMB"",[@[NOMBRE DE POINTS]]>='BDD Montant'!I5),'BDD Montant'!J5,AND([@SERVICE]=""EMB"",[@[NOMBRE DE POINTS]]>='BDD Montant'!I6),'BDD Montant'!J6,AND([@SERVICE]=""EMB"",[@[NOMBRE DE POINTS]]>=0),0

MAI :

AND([@SERVICE]=""MAI"",[@[NOMBRE DE POINTS]]>='BDD Montant'!K3),'BDD Montant'!L3,AND([@SERVICE]=""MAI"",[@[NOMBRE DE POINTS]]>='BDD Montant'!K4),'BDD Montant'!L4,AND([@SERVICE]=""MAI"",[@[NOMBRE DE POINTS]]>='BDD Montant'!K5),'BDD Montant'!L5,AND([@SERVICE]=""MAI"",[@[NOMBRE DE POINTS]]>='BDD Montant'!K6),'BDD Montant'!L6,AND([@SERVICE]=""MAI"",[@[NOMBRE DE POINTS]]>=0),0

NET :

AND([@SERVICE]=""NET"",[@[NOMBRE DE POINTS]]>='BDD Montant'!M3),'BDD Montant'!N3,AND([@SERVICE]=""NET"",[@[NOMBRE DE POINTS]]>='BDD Montant'!M4),'BDD Montant'!N4,AND([@SERVICE]=""NET"",[@[NOMBRE DE POINTS]]>='BDD Montant'!M5),'BDD Montant'!N5,AND([@SERVICE]=""NET"",[@[NOMBRE DE POINTS]]>='BDD Montant'!M6),'BDD Montant'!N6,AND([@SERVICE]=""NET"",[@[NOMBRE DE POINTS]]>=0),0

Cette fonction sert à attribuer des Majorations en fonction des attributs et des nombres de points car selon les attribut il n'y a pas les mêmes Majorations en fonction des points et en plus il n'y a pas la même quantité de points pour déclencher une Majoration.

Ex :

Règles de points pour attribut ABA :

Points : 100 / Majoration = 4000

Points : 70 / Majoration = 3000

Points = 40 / Majoration= 2000

Points = 10 / Majoration = 1000

en dessous de 10 point Majoration = 0

Règles de points pour attribut exp :

Points = 110 / Majoration = 3000

Points = 50 / Majoration = 2200

Points = 20 / Majoration = 1300

Points = 0 / Majoration = 0

J'ajoute un fichier exemple qui n'ai pas celui du code car j'ai anonymisé les données et les titres :

Je vous remercie d'avance pour votre aide.

Bonjour

pas certain d'avoir tout compris

mais pour la colonne "MAJORATION" de l'onglet "Analyses" je propose la formule

=SIERREUR(RECHERCHEX([@POINTS];
CHOISIRCOLS(TableauRéglesMarjoration;EQUIVX([@ATTRIBUT];'Regles Marjoration'!$A$1:$N$1));
CHOISIRCOLS(TableauRéglesMarjoration;EQUIVX([@ATTRIBUT];'Regles Marjoration'!$A$1:$N$1)+1);
0;-1);0)

à simplifier avec LET éventuellement

image

Stéphane

Merci beaucoup Raccourcix.

Cela est en effet beaucoup plus court et fonctionne.

Pourriez vous m'expliquer comment fonctionne la formule ?

J'ai compris les points suivant :

=(RECHERCHEX([@POINTS];

Recherche la colonne points dans l'onglet analyse

CHOISIRCOLS(TableauRéglesMarjoration;EQUIVX([@ATTRIBUT];'Regles Marjoration'!$A$1:$N$1))

dans le tableau de l'onglet "Regles Majoration" par rapport aux données dans la colonne "attribut" mais je ne sais pas de quel onglet il sagit ("Analyses" ou "Regles Majoration" ?)

CHOISIRCOLS(TableauRéglesMarjoration;EQUIVX([@ATTRIBUT];'Regles Marjoration'!$A$1:$N$1)

Je ne comprends pas pourquoi il y a une 2ème partie ChoisirCols et ce qu'elle fait.

+1);0;-1)

je ne comprends pas le +1);0;-1)

;0)

le dernier 0 permet de mettre 0 si la formule renvoie une erreur.

Merci du retour

EQUIVX([@ATTRIBUT];'Regles Marjoration'!$A$1:$N$1)

pour obtenir le n° de la colonne ABA = 1, DEC = 3, EXP = 5... jusqu'à NET = 13

le RECHERCHEX va chercher les points dans la première, troisième, cinquième ... colonne de TableauRéglesMarjoration et renvoyer la valeur correspondante dans la deuxième, quatrième, sixième... colonne (le +1)

ensuite 0 pour si non trouvé (si moins de 10 points pour ABA)

et -1 pour l'élément inférieur suivant (si j'ai 50 points pour ABA on veut la majoration 2000 € pour 40 points)

image

et en effet le dernier 0 pour le SIERREUR

Stéphane

Merci beaucoup pour ces explications. Je comprends mieux.

Bonjour Elena.t et raccourcix,,

Wow ! belle formule raccourcix, je n'avais pas pensé à ces nouvelles fonctions.

Comme j'y ai quand passé un certain temps, je joins ma proposition qui elle se base sur 2 colonnes de calculs en AA et AB (qui peuvent être masquées).

Pourquoi ces 2 colonnes : pour raccourcir la formule principale.

Cela ne sert probablement à rien sauf pour d'autres lecteurs qui n'auraient pas encore à disposition les fonctions que raccourcix a utilisées.

Bonne continuation

Chris

Bonjour

il y a plus simple que des INDIRECT ADRESSE : des INDEX avec les lignes à 0 pour renvoyer la ième colonne

INDEX(T_Marjoration;0;i)

cela donne avec la colonne intermédiaire AA proposée par CHRIS1945

=SIERREUR(INDEX(INDEX(T_Marjoration;0;AA2+1);EQUIV([@POINTS];INDEX(T_Marjoration;0;AA2);1));0)

et en direct sans colonne intermédiaire avec uniquement des INDEX et des EQUIV + un SIERREUR

=SIERREUR(
 INDEX(
   INDEX(T_Marjoration;0;EQUIV([@ATTRIBUT];'Regles Marjoration'!$A$1:$N$1;0)+1);
   EQUIV(
     [@POINTS];
     INDEX(T_Marjoration;0;EQUIV([@ATTRIBUT];'Regles Marjoration'!$A$1:$N$1;0));
     1)
   );
 0)

Stéphane

Rechercher des sujets similaires à "vba comment decomposer fonction depasse capacite"