Simplification de code...

belerofon

XLDnaute Occasionnel
Bonsoir les couche tard...
Je me pose une question existentielle...

Existe t il un moyen plus simple d ecrire ce morceau de code :

With Sheets("Feuil1")

A1C = Application.WorksheetFunction.CountIf(.Range("C1:C" & .Range("A65536").End(xlUp).Row), "X") * "0,01"
A2C = Application.WorksheetFunction.CountIf(.Range("D1:D" & .Range("A65536").End(xlUp).Row), "X") * "0,02"
A5C = Application.WorksheetFunction.CountIf(.Range("E1:E" & .Range("A65536").End(xlUp).Row), "X") * "0,05"
A10C = Application.WorksheetFunction.CountIf(.Range("F1:F" & .Range("A65536").End(xlUp).Row), "X") * "0,1"
A20C = Application.WorksheetFunction.CountIf(.Range("G1:G" & .Range("A65536").End(xlUp).Row), "X") * "0,2"
A50C = Application.WorksheetFunction.CountIf(.Range("H1:H" & .Range("A65536").End(xlUp).Row), "X") * "0,5"
A1E = Application.WorksheetFunction.CountIf(.Range("I1:I" & .Range("A65536").End(xlUp).Row), "X") * "1"
A2E = Application.WorksheetFunction.CountIf(.Range("J1:J" & .Range("A65536").End(xlUp).Row), "X") * "2"
AEC = Application.WorksheetFunction.CountIf(.Range("K1:K" & .Range("A65536").End(xlUp).Row), "X") * "2"

B1C = Application.WorksheetFunction.CountIf(.Range("M1:M" & .Range("A65536").End(xlUp).Row), "X") * "0,01"
B2C = Application.WorksheetFunction.CountIf(.Range("N1:N" & .Range("A65536").End(xlUp).Row), "X") * "0,02"
B5C = Application.WorksheetFunction.CountIf(.Range("O1:O" & .Range("A65536").End(xlUp).Row), "X") * "0,05"
B10C = Application.WorksheetFunction.CountIf(.Range("P1:p" & .Range("A65536").End(xlUp).Row), "X") * "0,1"
B20C = Application.WorksheetFunction.CountIf(.Range("Q1:Q" & .Range("A65536").End(xlUp).Row), "X") * "0,2"
B50C = Application.WorksheetFunction.CountIf(.Range("R1:R" & .Range("A65536").End(xlUp).Row), "X") * "0,5"
B1E = Application.WorksheetFunction.CountIf(.Range("S1:S" & .Range("A65536").End(xlUp).Row), "X") * "1"
B2E = Application.WorksheetFunction.CountIf(.Range("T1:T" & .Range("A65536").End(xlUp).Row), "X") * "2"
B2EC = Application.WorksheetFunction.CountIf(.Range("U1:U" & .Range("A65536").End(xlUp).Row), "X") * "2"
End With

If ComboBox1 = "A" Then
TextBox1.Value = VJUL1C + VJUL2C + VJUL5C + VJUL10C + VJUL20C + VJUL50C + VJUL1E + VJUL2E + VJUL2EC & " " & "€"
End If

If ComboBox1 = "B" Then
TextBox.Value = VBRI1C + VBRI2C + VBRI5C + VBRI10C + VBRI20C + VBRI50C + VBRI1E + VBRI2E + VBRI2EC & " " & "€"
End If

Peut être un cerveau moins embrumé que le mieux serait plus efficace...
Merci,
J.
 

Staple1600

XLDnaute Barbatruc
Re : Simplification de code...

bonjour belorofon , le fil, le forum

Pour nous simplifier la tâche de simplification de ton code, ne serait-il pas plus simple de joindre un fichier exemple tout simple, afin de nous permettre, à nous autres simples forumeurs de faire de test ?

En attendant ta pièce jointe, je m'en vais faire quelques pancakes (que j'espère simplement esquis) ;)
 

belerofon

XLDnaute Occasionnel
Re : Simplification de code...

tiens, coucou staples,
J espere qu il le seront et qu on pourra en gouter :)
Sinon pour le fichier exemple, il est consultable ici :https://www.excel-downloads.com/threads/renseignement-dun-tableau-grace-a-trois-variables.174415/
j'y ai passé la nuit, et suit venu a bout de presque toutes les difficultés ( enfin des difficultes pour moi :) ) je progresse doucement mais surement, bon par contre le code est peut etre un peu horrible vous formalisez pas :)
J.
 

Staple1600

XLDnaute Barbatruc
Re : Simplification de code...

Re

(Pssss: la charte ....pourquoi un deuxième fil, si j'avais connu le premier avec la pj, j'aurai pu testé ce que je viens de pondre à vue de nez (cf bas du message) , sans fichier.
Remplaces le code ci dessous par le code (non testé et pour cause :rolleyes:...) par celui que je te propose ci-dessous:
Code:
With Sheets("Feuil1")

A1C = Application.WorksheetFunction.CountIf(.Range("C1:C  " & .Range("A65536").End(xlUp).Row), "X") * "0,01"
A2C = Application.WorksheetFunction.CountIf(.Range("D1:D" & .Range("A65536").End(xlUp).Row), "X") * "0,02"
A5C = Application.WorksheetFunction.CountIf(.Range("E1:E  " & .Range("A65536").End(xlUp).Row), "X") * "0,05"
A10C = Application.WorksheetFunction.CountIf(.Range("F1:F  " & .Range("A65536").End(xlUp).Row), "X") * "0,1"
A20C = Application.WorksheetFunction.CountIf(.Range("G1:G  " & .Range("A65536").End(xlUp).Row), "X") * "0,2"
A50C = Application.WorksheetFunction.CountIf(.Range("H1:H  " & .Range("A65536").End(xlUp).Row), "X") * "0,5"
A1E = Application.WorksheetFunction.CountIf(.Range("I1:I  " & .Range("A65536").End(xlUp).Row), "X") * "1"
A2E = Application.WorksheetFunction.CountIf(.Range("J1:J  " & .Range("A65536").End(xlUp).Row), "X") * "2"
AEC = Application.WorksheetFunction.CountIf(.Range("K1:K  " & .Range("A65536").End(xlUp).Row), "X") * "2"

B1C = Application.WorksheetFunction.CountIf(.Range("M1:M  " & .Range("A65536").End(xlUp).Row), "X") * "0,01"
B2C = Application.WorksheetFunction.CountIf(.Range("N1:N  " & .Range("A65536").End(xlUp).Row), "X") * "0,02"
B5C = Application.WorksheetFunction.CountIf(.Range("O1:O  " & .Range("A65536").End(xlUp).Row), "X") * "0,05"
B10C = Application.WorksheetFunction.CountIf(.Range("P1:P  " & .Range("A65536").End(xlUp).Row), "X") * "0,1"
B20C = Application.WorksheetFunction.CountIf(.Range("Q1:Q  " & .Range("A65536").End(xlUp).Row), "X") * "0,2"
B50C = Application.WorksheetFunction.CountIf(.Range("R1:R  " & .Range("A65536").End(xlUp).Row), "X") * "0,5"
B1E = Application.WorksheetFunction.CountIf(.Range("S1:S  " & .Range("A65536").End(xlUp).Row), "X") * "1"
B2E = Application.WorksheetFunction.CountIf(.Range("T1:T  " & .Range("A65536").End(xlUp).Row), "X") * "2"
B2EC = Application.WorksheetFunction.CountIf(.Range("U1:U  " & .Range("A65536").End(xlUp).Row), "X") * "2"
End With
VB:
Dim dlg&, i As Byte, tBlo, Tvals

    tBlo = _
        Split("A1C/A2C/A5C/A10C/A20C/A50C/A1E/A2E/AEC/B1C/B2C/B5C/B10C/B20C/B50C/B1E/B2EC", "/")
    Tvals = _
    Array(0.01, 0.02, 0.05, 0.1, 0.2, 0.5, 1, 2, 2, 0.01, 0.02, 0.05, 0.1, 0.2, 0.5, 1, 2, 2)

With Sheets("Feuil1")
dlg = .Range("A65536").End(xlUp).Row
    With Application.WorksheetFunction
        For i = 1 To 18
        Select Case i
        Case 3 To 11, 13 To 21
        tBlo(i) = .CountIf(.Cells(1, i).Resize(dlg), "X") * Tvals(i)
        End Select
    Next i
    End With
End With


Je te laisse donc tester ce qui suit donc , pendant que que je bois mon café et fume ma cibiche du matin. ;
 
Dernière édition:

belerofon

XLDnaute Occasionnel
Re : Simplification de code...

ben je pensais qu un autre sujet meritait un nouveau post, ne serait ce que pour le referencement, excel download est bien referencé sur la gogole et ca permet a plein de gens de trouver plus facilement l information qu il cherche de facon precise , ne crois tu pas?
 

Staple1600

XLDnaute Barbatruc
Re : Simplification de code...

RE

vois mon précédent message , je t'ai proposé une simplification

PS: relis Lien supprimé et tu verras ce qui dit à propos de la pertinence de joindre une pièce joindre pour illustrer la question du demandeur
 

belerofon

XLDnaute Occasionnel
Re : Simplification de code...

Rooooh staples,
je sais bien , regarde mes archives et tu verras que sur les 20 derniers messages que j ai du poster il doit y en avoir 18 avec des fichiers exemples , mis a jours a chaque avancée , mauvaise foi va, ca te couedtera un pancake ;)
 

Staple1600

XLDnaute Barbatruc
Re : Simplification de code...

Re

Un autre prendra la suite, car là je pars au taf
etautafbahpasdxld ;) .Bonne journée
PS: je reviendrai dans le fil ce soir

EDITION: De retour comme promis.
Je suis allé voir la PJ de ton autre fil :rolleyes:
J'attends pour aller plus loin que tu postes dans ce fil une pièce jointe* dont le code VBA est bien en adéquation avec les données que contient le classeur.

*: il est de coutume sur XLD que ce soit l'initiateur du fil qui propose et/ou adapte sa pièce jointe dans le fil discussion qu'il s'apprête à créer.
Quand c'est le cas (et ce n'est pas le cas ici), c'est avec plaisir que je passe en "mode répondeur" sur le forum pour aider le demandeur.
J'attends donc que tu prennes le temps d'adapter ta pièce jointe et que l'insères dans cette discussion .
Dès que ce sera fait, je pourrai tester mes propositions de simplification de ton code.
La balle est dans ton camp ;)
Un indice :
La PJ de ton autre fil :
VB:
' Méthode Calcul de la valeur de la collection
With Sheets("Base")
VJUL1C = Application.WorksheetFunction.CountIf(.Range("C1:C" & .Range("A65536").End(xlUp).Row), "X") * "0,01"
VBRI1C = Application.WorksheetFunction.CountIf(.Range("M1:M" & .Range("A65536").End(xlUp).Row), "X") * "0,01"
VJUL2C = Application.WorksheetFunction.CountIf(.Range("D1:D" & .Range("A65536").End(xlUp).Row), "X") * "0,02"
VBRI2C = Application.WorksheetFunction.CountIf(.Range("N1:N" & .Range("A65536").End(xlUp).Row), "X") * "0,02"
VJUL5C = Application.WorksheetFunction.CountIf(.Range("E1:E" & .Range("A65536").End(xlUp).Row), "X") * "0,05"
VBRI5C = Application.WorksheetFunction.CountIf(.Range("O1:O" & .Range("A65536").End(xlUp).Row), "X") * "0,05"
VJUL10C = Application.WorksheetFunction.CountIf(.Range("F1:F" & .Range("A65536").End(xlUp).Row), "X") * "0,1"
VBRI10C = Application.WorksheetFunction.CountIf(.Range("P1:P" & .Range("A65536").End(xlUp).Row), "X") * "0,1"
VJUL20C = Application.WorksheetFunction.CountIf(.Range("G1:G" & .Range("A65536").End(xlUp).Row), "X") * "0,2"
VBRI20C = Application.WorksheetFunction.CountIf(.Range("Q1:Q" & .Range("A65536").End(xlUp).Row), "X") * "0,2"
VJUL50C = Application.WorksheetFunction.CountIf(.Range("H1:H" & .Range("A65536").End(xlUp).Row), "X") * "0,5"
VBRI50C = Application.WorksheetFunction.CountIf(.Range("R1:R" & .Range("A65536").End(xlUp).Row), "X") * "0,5"
VJUL1E = Application.WorksheetFunction.CountIf(.Range("I1:I" & .Range("A65536").End(xlUp).Row), "X") * "1"
VBRI1E = Application.WorksheetFunction.CountIf(.Range("S1:S" & .Range("A65536").End(xlUp).Row), "X") * "1"
VJUL2E = Application.WorksheetFunction.CountIf(.Range("J1:J" & .Range("A65536").End(xlUp).Row), "X") * "2"
VBRI2E = Application.WorksheetFunction.CountIf(.Range("T1:T" & .Range("A65536").End(xlUp).Row), "X") * "2"
VJUL2EC = Application.WorksheetFunction.CountIf(.Range("K1:K" & .Range("A65536").End(xlUp).Row), "X") * "2"
VBRI2EC = Application.WorksheetFunction.CountIf(.Range("U1:U" & .Range("A65536").End(xlUp).Row), "X") * "2"
End With

L'extrait de code de ton premier message ici:
VB:
With Sheets("Feuil1")

A1C = Application.WorksheetFunction.CountIf(.Range("C1:C  " & .Range("A65536").End(xlUp).Row), "X") * "0,01"
A2C = Application.WorksheetFunction.CountIf(.Range("D1:D" & .Range("A65536").End(xlUp).Row), "X") * "0,02"
A5C = Application.WorksheetFunction.CountIf(.Range("E1:E  " & .Range("A65536").End(xlUp).Row), "X") * "0,05"
A10C = Application.WorksheetFunction.CountIf(.Range("F1:F  " & .Range("A65536").End(xlUp).Row), "X") * "0,1"
A20C = Application.WorksheetFunction.CountIf(.Range("G1:G  " & .Range("A65536").End(xlUp).Row), "X") * "0,2"
A50C = Application.WorksheetFunction.CountIf(.Range("H1:H  " & .Range("A65536").End(xlUp).Row), "X") * "0,5"
A1E = Application.WorksheetFunction.CountIf(.Range("I1:I  " & .Range("A65536").End(xlUp).Row), "X") * "1"
A2E = Application.WorksheetFunction.CountIf(.Range("J1:J  " & .Range("A65536").End(xlUp).Row), "X") * "2"
AEC = Application.WorksheetFunction.CountIf(.Range("K1:K  " & .Range("A65536").End(xlUp).Row), "X") * "2"

B1C = Application.WorksheetFunction.CountIf(.Range("M1:M  " & .Range("A65536").End(xlUp).Row), "X") * "0,01"
B2C = Application.WorksheetFunction.CountIf(.Range("N1:N  " & .Range("A65536").End(xlUp).Row), "X") * "0,02"
B5C = Application.WorksheetFunction.CountIf(.Range("O1:O  " & .Range("A65536").End(xlUp).Row), "X") * "0,05"
B10C = Application.WorksheetFunction.CountIf(.Range("P1:P  " & .Range("A65536").End(xlUp).Row), "X") * "0,1"
B20C = Application.WorksheetFunction.CountIf(.Range("Q1:Q  " & .Range("A65536").End(xlUp).Row), "X") * "0,2"
B50C = Application.WorksheetFunction.CountIf(.Range("R1:R  " & .Range("A65536").End(xlUp).Row), "X") * "0,5"
B1E = Application.WorksheetFunction.CountIf(.Range("S1:S  " & .Range("A65536").End(xlUp).Row), "X") * "1"
B2E = Application.WorksheetFunction.CountIf(.Range("T1:T  " & .Range("A65536").End(xlUp).Row), "X") * "2"
B2EC = Application.WorksheetFunction.CountIf(.Range("U1:U  " & .Range("A65536").End(xlUp).Row), "X") * "2"
End With

If ComboBox1 = "A" Then
TextBox1.Value = VJUL1C + VJUL2C + VJUL5C + VJUL10C + VJUL20C + VJUL50C + VJUL1E + VJUL2E + VJUL2EC & " " & "€"
End If

If ComboBox1 = "B" Then
TextBox.Value = VBRI1C + VBRI2C + VBRI5C + VBRI10C + VBRI20C + VBRI50C + VBRI1E + VBRI2E + VBRI2EC & " " & "€"
End If
 
Dernière édition:

belerofon

XLDnaute Occasionnel
Re : Simplification de code...

Staple, resalut,
Comme mentionné dans l autre fil , je n avait pas vu ton MP, et vu que tu avait fait un Edit sur ce fil , il n est pas remonté dans le forum et donc j ignorais qu il avait ete edité, d ou mon absence de nouvelles sur ce fil...
Ceci dit je t ai expliqué les raisons du changement de code sur l autre fil , dans un but d optimisation du fichier que je fabrique...
Et pour en revenir à la charte, je ne pense pas que la charte preconise de poster le meme fichier exemple dans deux fils differents concernant deux topics differents... ca encombre les serveurs et ca contribue au rechauffement de la planete indirectement...
 

Staple1600

XLDnaute Barbatruc
Re : Simplification de code...

RE


On s'est mal compris, je crois

Je veux simplement dire que c'est (à mon sens) au demandeur de faire l'effort d'éditer son code VBA d'une PJ à une autre.

Dans le cas présent, ma proposition de simplification fonctionne avec une PJ qui aurait une feuille nommée Feuil1 (or dans ta PJ de l'autre fil, elle se nomme Base)

Et comme je me suis basé sur ton code présent dans ton premier message de ce fil
L'array nommée tBlo contient des valeurs en rapport avec l'exemple de ce fil
tBlo = _
Split("A1C/A2C/A5C/A10C/A20C/A50C/A1E/A2E/AEC/B1C/B2C/B5C/B10C/B20C/B50C/B1E/B2EC"
or dans la PJ les valeurs sont différentes:
VJUL1C,VBRI1C,VJUL2C,...VBRI2EC
Je te suggérais donc de prendre le temps de renommer ces variables dans une nouvelle PJ.

Et cela simplement parce c'est pour moi , une bonne façon pour le "demandeur" de comprendre le fonctionnent des codes proposés par les "répondeurs".

J'espère que ce message t'aura aidé à comprendre le sens du précédent.
 
Dernière édition:

belerofon

XLDnaute Occasionnel
Re : Simplification de code...

Staple,
meme si je ne suis pas un cador en VB , j avais evidemment corriger les noms de variables tels que le nom de la feuille et les variables renommées... quand même... mais bon j ai peut etre negligé un detail, je recommence pour verifier...
 

belerofon

XLDnaute Occasionnel
Re : Simplification de code...

Staple,
J'ai refait le test et donc confirmation que le code amene au declenchement du debuggeur.

Je te rejoint le fichier exemple pour plus de praticité, effectivement :

Edit : Fichier mis a jour a Minuit ( Pu de code Workbook_Open() )
 

Pièces jointes

  • Euros Projet.xls
    245 KB · Affichages: 40
  • Euros Projet.xls
    245 KB · Affichages: 40
  • Euros Projet.xls
    245 KB · Affichages: 43
Dernière édition:

Statistiques des forums

Discussions
312 763
Messages
2 091 826
Membres
105 076
dernier inscrit
simeand