variable pour effectuer une boucle sur une plage précise

CHRISTOPHEAGPM

XLDnaute Nouveau
bonjour à tous,

Petite modif de mon 1er message pour le rendre plus compréhensible :


L'année dernière Bebere et Eriiiic m'avait donné un sacré coup de main sur la discussion suivante :

https://www.excel-downloads.com/threads/rempacer-sommeprod-par-un-code-vba.148362/


Aujourd'hui je vous sollicite à nouveau pour m'aider à transformer les lignes :

M = .range("G65536").end(xlUP).row

et

Derl = .range("G65536").end(xlUP).row

Pour définir deux variablesL permettant d'effectuer les calculs uniquement sur les Plages suivantes par exemple (dans les faits il y'en à 12, mais si je comprends celles-ci...):

G1155-G2244
G2300-G3389
...

Merci d'avance pour votre aide.

Christophe.
 

Pièces jointes

  • recap.zip
    266.9 KB · Affichages: 24
  • recap.zip
    266.9 KB · Affichages: 25
  • recap.zip
    266.9 KB · Affichages: 27
Dernière édition:

flyonets44

XLDnaute Occasionnel
Re : variable pour effectuer une boucle sur une plage précise

Bonjour
Dim rnga as range
Dim rngb as range, Cll as variant
set rnga =range("g1155: g2244")
set rngb =range("g2300: g3389")
For each cll in rnga
ton code
next
For each cll in rngb
ton code
next
set rnga=nothing : set rngb=nothing
 

Staple1600

XLDnaute Barbatruc
Re : variable pour effectuer une boucle sur une plage précise

Bonsoir

En ajoutant des oignons, ca donne du gout
(et ca allège le code ;) , car moins de boucles)
Code:
Sub ranges_avec_oignons()
Dim mes_plages As Range, c As Range
Set mes_plages = Union([A1:A3], [c1:c3], [e1:f3], [k1:k3], [l1:n3])
For Each c In mes_plages
c.Interior.ColorIndex = 9
Next c
End Sub

A adapter avec tes propres plages
 

CHRISTOPHEAGPM

XLDnaute Nouveau
Re : variable pour effectuer une boucle sur une plage précise

Merci Staple1600 et flyonets44,

Je ne parviens pas à integrer vos propositions à mon projet.
Je vous joins un exemple du classeur réduit à sa plus simple expression.

J'arrive à effectuer les calculs pour janvier (onglet récapitulatif de la ligne 10 à 1099)
mais à partie de février (1155 à 2244) et pour les autres mois je ne comprends pas comment faire ???

Je pense qu'il faut jouer sur la définition de la variable "M" et "Derl" mais comment ????

VB:
 M= .range("G1099").end(xlUp).row

VB:
 Derl= .range("G1099").end(xlUp).row

Si l'un d'entre vous peut me faire bénéficier de ses lumières je lui en serait tres reconnaissant.

Merci beaucoup.
 

Pièces jointes

  • recap.zip
    266.9 KB · Affichages: 38
  • recap.zip
    266.9 KB · Affichages: 36
  • recap.zip
    266.9 KB · Affichages: 32

CHRISTOPHEAGPM

XLDnaute Nouveau
Re : variable pour effectuer une boucle sur une plage précise

Merci Staple1600 ,

je modifie ce premier message pour le rendre plus compréhensible.
Ce qui se conçoit bien s'énonce clairement, à ce qu'il parait... je doit pas vraiment bien concevoir :p

Merci à tous
 

flyonets44

XLDnaute Occasionnel
Re : variable pour effectuer une boucle sur une plage précise

Bonjour
Ta question serait nettement plus facile à traiter si tu crées 12 feuilles mensuelles strictement identiques et une feuille recap pour
ensuite consolider tes données de base
Bon dimanche
Flyonets
 

CHRISTOPHEAGPM

XLDnaute Nouveau
Re : variable pour effectuer une boucle sur une plage précise

Bonjour à tous, et merci de vous etre penché sur mon problème.
J'ai finalement réussi à le résoudre et poste ici le résultat de ma reflexion en esperant qu'il puisse un jour servir à quelqu'un.

Si il est possible de le réduire, et que l'un d'entre vous à le courage de s'y pencher je lui en serait reconnaissant. Mais ça fonctionne.

Voici donc le code utilisé :

Code:
Public Sub Activité_Unités_janvier()

    Dim rng As Range, M As Integer

    With Worksheets("récapitulatif")
    
    M = .Range("G10").End(xlUp).Row
       
        
        Set rng = .Range("G1099:G" & M)
        rng.Name = "ColG"
        Set rng = .Range("I1099:I" & M)
        rng.Name = "ColI"
        Set rng = .Range("H1099:H" & M)
        rng.Name = "ColH"
        Set rng = .Range("BH1099:BH" & M)
        rng.Name = "ColBH"

    End With

    Set rng = Nothing



    Dim L As Long, DerL As Long, E As String, B As String, C As String
    
   Application.ScreenUpdating = False
    With Worksheets("Production Unités")
        DerL = .Range("G1099").End(xlUp).Row
        
        
        E = .Range("f3") 'variable origine 1
        B = .Range("B4") 'variable am ou qualité 1
        D = .Range("B59") 'variable am ou qualité 2
        F = .Range("B114") 'variable am ou qualité 3
        G = .Range("B169") 'variable am ou qualité 4
        H = .Range("B225") 'variable am ou qualité 5
        
        For L = 7 To 55
        
            E = .Range("f3"): C = .Range("C" & L)
            .Cells(L, 6) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & B & """))")
            
            E = .Range("G3")
            .Cells(L, 7) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & B & """))")
            
            E = .Range("H3")
            .Cells(L, 8) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & B & """))")
            
            E = .Range("I3")
            .Cells(L, 9) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & B & """))")
            
            E = .Range("J3")
            .Cells(L, 10) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & B & """))")
            
            E = .Range("K3")
            .Cells(L, 11) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & B & """))")
            
            E = .Range("L3")
            .Cells(L, 12) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & B & """))")
            
            E = .Range("M3")
            .Cells(L, 13) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & B & """))")
            
            E = .Range("N3")
            .Cells(L, 14) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & B & """))")
            
            E = .Range("O3")
            .Cells(L, 15) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & B & """))")
            
            E = .Range("P3")
            .Cells(L, 16) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & B & """))")
            
            E = .Range("Q3")
            .Cells(L, 17) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & B & """))")
            
            E = .Range("R3")
            .Cells(L, 18) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & B & """))")
            
            E = .Range("S3")
            .Cells(L, 19) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & B & """))")
            
            E = .Range("T3")
            .Cells(L, 20) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & B & """))")
            
            E = .Range("U3")
            .Cells(L, 21) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & B & """))")
            
            E = .Range("V3")
            .Cells(L, 22) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & B & """))")
            
            Next
            
            ' deuxieme choix am ou qualité
            
            For L = 62 To 110
            
            
            E = .Range("f3"): C = .Range("C" & L)
            .Cells(L, 6) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & D & """))")
            
            E = .Range("G3")
            .Cells(L, 7) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & D & """))")
            
            E = .Range("H3")
            .Cells(L, 8) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & D & """))")
            
            E = .Range("I3")
            .Cells(L, 9) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & D & """))")
            
            E = .Range("J3")
            .Cells(L, 10) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & D & """))")
            
            E = .Range("K3")
            .Cells(L, 11) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & D & """))")
            
            E = .Range("L3")
            .Cells(L, 12) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & D & """))")
            
            E = .Range("M3")
            .Cells(L, 13) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & D & """))")
            
            E = .Range("N3")
            .Cells(L, 14) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & D & """))")
            
            E = .Range("O3")
            .Cells(L, 15) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & D & """))")
            
            E = .Range("P3")
            .Cells(L, 16) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & D & """))")
            
            E = .Range("Q3")
            .Cells(L, 17) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & D & """))")
            
            E = .Range("R3")
            .Cells(L, 18) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & D & """))")
            
            E = .Range("S3")
            .Cells(L, 19) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & D & """))")
            
            E = .Range("T3")
            .Cells(L, 20) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & D & """))")
            
            E = .Range("U3")
            .Cells(L, 21) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & D & """))")
            
            E = .Range("V3")
            .Cells(L, 22) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & D & """))")
            
            
            
        Next
        
        ' troisieme choix am ou qualité
            
            For L = 117 To 165
            
            
            E = .Range("f3"): C = .Range("C" & L)
            .Cells(L, 6) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & F & """))")
            
            E = .Range("G3")
            .Cells(L, 7) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & F & """))")
            
            E = .Range("H3")
            .Cells(L, 8) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & F & """))")
            
            E = .Range("I3")
            .Cells(L, 9) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & F & """))")
            
            E = .Range("J3")
            .Cells(L, 10) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & F & """))")
            
            E = .Range("K3")
            .Cells(L, 11) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & F & """))")
            
            E = .Range("L3")
            .Cells(L, 12) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & F & """))")
            
            E = .Range("M3")
            .Cells(L, 13) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & F & """))")
            
            E = .Range("N3")
            .Cells(L, 14) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & F & """))")
            
            E = .Range("O3")
            .Cells(L, 15) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & F & """))")
            
            E = .Range("P3")
            .Cells(L, 16) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & F & """))")
            
            E = .Range("Q3")
            .Cells(L, 17) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & F & """))")
            
            E = .Range("R3")
            .Cells(L, 18) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & F & """))")
            
            E = .Range("S3")
            .Cells(L, 19) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & F & """))")
            
            E = .Range("T3")
            .Cells(L, 20) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & F & """))")
            
            E = .Range("U3")
            .Cells(L, 21) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & F & """))")
            
            E = .Range("V3")
            .Cells(L, 22) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & F & """))")
            
            
            
        Next
        
        
        ' quatrieme choix am ou qualité
            
            For L = 172 To 220
            
            
            E = .Range("f3"): C = .Range("C" & L)
            .Cells(L, 6) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & G & """))")
            
            E = .Range("G3")
            .Cells(L, 7) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & G & """))")
            
            E = .Range("H3")
            .Cells(L, 8) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & G & """))")
            
            E = .Range("I3")
            .Cells(L, 9) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & G & """))")
            
            E = .Range("J3")
            .Cells(L, 10) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & G & """))")
            
            E = .Range("K3")
            .Cells(L, 11) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & G & """))")
            
            E = .Range("L3")
            .Cells(L, 12) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & G & """))")
            
            E = .Range("M3")
            .Cells(L, 13) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & G & """))")
            
            E = .Range("N3")
            .Cells(L, 14) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & G & """))")
            
            E = .Range("O3")
            .Cells(L, 15) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & G & """))")
            
            E = .Range("P3")
            .Cells(L, 16) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & G & """))")
            
            E = .Range("Q3")
            .Cells(L, 17) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & G & """))")
            
            E = .Range("R3")
            .Cells(L, 18) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & G & """))")
            
            E = .Range("S3")
            .Cells(L, 19) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & G & """))")
            
            E = .Range("T3")
            .Cells(L, 20) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & G & """))")
            
            E = .Range("U3")
            .Cells(L, 21) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & G & """))")
            
            E = .Range("V3")
            .Cells(L, 22) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & G & """))")
            
            
            
        Next
        
            ' cinquieme choix am ou qualité
            
            For L = 227 To 275
            
            
            E = .Range("f3"): C = .Range("C" & L)
            .Cells(L, 6) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & H & """))")
            
            E = .Range("G3")
            .Cells(L, 7) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & H & """))")
            
            E = .Range("H3")
            .Cells(L, 8) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & H & """))")
            
            E = .Range("I3")
            .Cells(L, 9) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & H & """))")
            
            E = .Range("J3")
            .Cells(L, 10) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & H & """))")
            
            E = .Range("K3")
            .Cells(L, 11) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & H & """))")
            
            E = .Range("L3")
            .Cells(L, 12) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & H & """))")
            
            E = .Range("M3")
            .Cells(L, 13) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & H & """))")
            
            E = .Range("N3")
            .Cells(L, 14) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & H & """))")
            
            E = .Range("O3")
            .Cells(L, 15) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & H & """))")
            
            E = .Range("P3")
            .Cells(L, 16) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & H & """))")
            
            E = .Range("Q3")
            .Cells(L, 17) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & H & """))")
            
            E = .Range("R3")
            .Cells(L, 18) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & H & """))")
            
            E = .Range("S3")
            .Cells(L, 19) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & H & """))")
            
            E = .Range("T3")
            .Cells(L, 20) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & H & """))")
            
            E = .Range("U3")
            .Cells(L, 21) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & H & """))")
            
            E = .Range("V3")
            .Cells(L, 22) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & H & """))")
            
            
            
        Next
        
 Application.ScreenUpdating = True
 
    End With

End Sub
 
C

Compte Supprimé 979

Guest
Re : variable pour effectuer une boucle sur une plage précise

Bonjour Christophe,

On peut effectivement, largement optimiser ton code ;)

VB:
Public Sub Activité_Unités_janvier()
  Dim rng As Range, M As Integer
  Dim L As Long, DerL As Long, E As String, B As String, C As String
  Dim Col As Integer
  With Worksheets("récapitulatif")
    M = .Range("G10").End(xlUp).Row
    Set rng = .Range("G1099:G" & M)
    rng.Name = "ColG"
    Set rng = .Range("I1099:I" & M)
    rng.Name = "ColI"
    Set rng = .Range("H1099:H" & M)
    rng.Name = "ColH"
    Set rng = .Range("BH1099:BH" & M)
    rng.Name = "ColBH"
  End With
  Set rng = Nothing
  Application.ScreenUpdating = False
  With Worksheets("Production Unités")
    DerL = .Range("G1099").End(xlUp).Row
    E = .Range("f3")  'variable origine 1
    B = .Range("B4")  'variable am ou qualité 1
    D = .Range("B59")  'variable am ou qualité 2
    F = .Range("B114")  'variable am ou qualité 3
    G = .Range("B169")  'variable am ou qualité 4
    H = .Range("B225")  'variable am ou qualité 5
    For L = 7 To 275
      C = .Range("C" & L)
      For Col = 6 To 22   ' Colonne F à V
        E = .Cells(3, Col)
        Select Case L
        Case 7 To 55
          .Cells(L, Col) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & B & """))")
        Case 62 To 110  ' deuxieme choix am ou qualité
          .Cells(L, Col) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & D & """))")
        Case 117 To 165 ' troisieme choix am ou qualité
          .Cells(L, Col) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & F & """))")
        Case 172 To 220 ' quatrieme choix am ou qualité
          .Cells(L, Col) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & G & """))")
        Case 227 To 275 ' cinquieme choix am ou qualité
          .Cells(L, Col) = Evaluate("sumproduct((ColG=""" & E & """)*(ColI=""" & C & """)*(ColH=""" & H & """))")
        End Select
      Next Col
    Next L
    Application.ScreenUpdating = True
  End With
End Sub

A+
 

Discussions similaires

Statistiques des forums

Discussions
312 489
Messages
2 088 852
Membres
103 974
dernier inscrit
chmikha