Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

répétition de code sur plusieurs feuilles

1 réponse
Avatar
bourgouinkristof
Bonjour =E0 tous,
dans la macro que j'ai effectu=E9e ... (grace =E0 l'aide de certains) j'ai
des instructions qui se r=E9p=E8tent pour certaines feuilles.
y a t'il la possibilit=E9 de mettre ces lignes de codes dans une sorte
de sous programme ?
Le code suivant est =E0 faire pour la feuille REGION 1 =E0 REGION 8 par
exemple
Merci de vos r=E9ponses

Sheets("REGION 1").Select
'=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D
'Totaux Y
nby =3D Application.WorksheetFunction.CountIf(Range("G1:G200"), "Y
")
ligty =3D nby + 2
debny =3D ligty + 2
Application.Goto Range("A" & ligty)
Range(ActiveCell, ActiveCell.Offset(1, 20)).Select
Selection.Insert Shift:=3DxlDown
Range("F" & ligty) =3D "TOTAL Y"
Application.Goto Range("H" & ligty)
ActiveCell.Formula =3D "=3DSUBTOTAL(9,H2:H" & ActiveCell.Row - 1 & ")"
Application.Goto Range("I" & ligty)
ActiveCell.Formula =3D "=3DSUBTOTAL(9,I2:I" & ActiveCell.Row - 1 & ")"
Application.Goto Range("J" & ligty)
ActiveCell.Formula =3D "=3DSUBTOTAL(9,J2:J" & ActiveCell.Row - 1 & ")"
Application.Goto Range("L" & ligty)
ActiveCell.Formula =3D "=3DSUBTOTAL(9,L2:L" & ActiveCell.Row - 1 & ")"
Application.Goto Range("N" & ligty)
ActiveCell.Formula =3D "=3DSUBTOTAL(9,N2:N" & ActiveCell.Row - 1 & ")"
Application.Goto Range("P" & ligty)
ActiveCell.Formula =3D "=3DSUBTOTAL(9,P2:P" & ActiveCell.Row - 1 & ")"
Application.Goto Range("R" & ligty)
ActiveCell.Formula =3D "=3DSUBTOTAL(9,R2:R" & ActiveCell.Row - 1 & ")"
Application.Goto Range("S" & ligty)
ActiveCell.Formula =3D "=3DSUBTOTAL(9,S2:S" & ActiveCell.Row - 1 & ")"
Application.Goto Range("K" & ligty)
ActiveCell.FormulaR1C1 =3D "=3DIF(RC[-3]<>0,RC[-1]/ABS(RC[-3]),"" "")"
Application.Goto Range("M" & ligty)
ActiveCell.FormulaR1C1 =3D "=3DIF(RC[-5]<>0,RC[-1]/ABS(RC[-5]),"" "")"
Application.Goto Range("O" & ligty)
ActiveCell.FormulaR1C1 =3D "=3DIF(RC[-7]<>0,RC[-1]/ABS(RC[-7]),"" "")"
Application.Goto Range("Q" & ligty)
ActiveCell.FormulaR1C1 =3D "=3DIF(RC[-9]<>0,RC[-1]/ABS(RC[-9]),"" "")"
Application.Goto Range("T" & ligty)
ActiveCell.FormulaR1C1 =3D "=3DIF(RC[-11]<>0,RC[-12]/ABS(RC[-11]),""
"")"
'Mise en Gras
Range("A" & ligty, "T" & ligty).Select
Selection.Font.Bold =3D True
'=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D
'Totaux NY
nbny =3D Application.WorksheetFunction.CountIf(Range("G1:G200"),
"NY")
ligtny =3D nbny + 2 + ligty
ligtot =3D ligtny + 2
Application.Goto Range("A" & ligtny)
Range(ActiveCell, ActiveCell.Offset(1, 20)).Select
Selection.Insert Shift:=3DxlDown
Range("F" & ligtny) =3D "TOTAL NY"
Application.Goto Range("H" & ligtny)
ActiveCell.Formula =3D "=3DSUBTOTAL(9,H" & debny & ":H" &
ActiveCell.Row - 1 & ")"
Application.Goto Range("I" & ligtny)
ActiveCell.Formula =3D "=3DSUBTOTAL(9,I" & debny & ":I" &
ActiveCell.Row - 1 & ")"
Application.Goto Range("J" & ligtny)
ActiveCell.Formula =3D "=3DSUBTOTAL(9,J" & debny & ":J" &
ActiveCell.Row - 1 & ")"
Application.Goto Range("L" & ligtny)
ActiveCell.Formula =3D "=3DSUBTOTAL(9,L" & debny & ":L" &
ActiveCell.Row - 1 & ")"
Application.Goto Range("N" & ligtny)
ActiveCell.Formula =3D "=3DSUBTOTAL(9,N" & debny & ":N" &
ActiveCell.Row - 1 & ")"
Application.Goto Range("P" & ligtny)
ActiveCell.Formula =3D "=3DSUBTOTAL(9,P" & debny & ":P" &
ActiveCell.Row - 1 & ")"
Application.Goto Range("R" & ligtny)
ActiveCell.Formula =3D "=3DSUBTOTAL(9,R" & debny & ":R" &
ActiveCell.Row - 1 & ")"
Application.Goto Range("S" & ligtny)
ActiveCell.Formula =3D "=3DSUBTOTAL(9,S" & debny & ":S" &
ActiveCell.Row - 1 & ")"
Application.Goto Range("K" & ligtny)
ActiveCell.FormulaR1C1 =3D "=3DIF(RC[-3]<>0,RC[-1]/ABS(RC[-3]),"" "")"
Application.Goto Range("M" & ligtny)
ActiveCell.FormulaR1C1 =3D "=3DIF(RC[-5]<>0,RC[-1]/ABS(RC[-5]),"" "")"
Application.Goto Range("O" & ligtny)
ActiveCell.FormulaR1C1 =3D "=3DIF(RC[-7]<>0,RC[-1]/ABS(RC[-7]),"" "")"
Application.Goto Range("Q" & ligtny)
ActiveCell.FormulaR1C1 =3D "=3DIF(RC[-9]<>0,RC[-1]/ABS(RC[-9]),"" "")"
Application.Goto Range("T" & ligtny)
ActiveCell.FormulaR1C1 =3D "=3DIF(RC[-11]<>0,RC[-12]/ABS(RC[-11]),""
"")"
'Mise en gras
Range("A" & ligtny, "T" & ligtny).Select
Selection.Font.Bold =3D True
'=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D
'Totaux Y + NY
Range("F" & ligtot) =3D "TOTAL REGION"
Range("H" & ligtot) =3D Range("H" & ligty) + Range("H" & ligtny)
Range("I" & ligtot) =3D Range("I" & ligty) + Range("I" & ligtny)
Range("J" & ligtot) =3D Range("J" & ligty) + Range("J" & ligtny)
Range("L" & ligtot) =3D Range("L" & ligty) + Range("L" & ligtny)
Range("N" & ligtot) =3D Range("N" & ligty) + Range("N" & ligtny)
Range("P" & ligtot) =3D Range("P" & ligty) + Range("P" & ligtny)
Range("R" & ligtot) =3D Range("R" & ligty) + Range("R" & ligtny)
Range("S" & ligtot) =3D Range("S" & ligty) + Range("S" & ligtny)
Application.Goto Range("K" & ligtot)
ActiveCell.FormulaR1C1 =3D "=3DIF(RC[-3]<>0,RC[-1]/ABS(RC[-3]),"" "")"
Application.Goto Range("M" & ligtot)
ActiveCell.FormulaR1C1 =3D "=3DIF(RC[-5]<>0,RC[-1]/ABS(RC[-5]),"" "")"
Application.Goto Range("O" & ligtot)
ActiveCell.FormulaR1C1 =3D "=3DIF(RC[-7]<>0,RC[-1]/ABS(RC[-7]),"" "")"
Application.Goto Range("Q" & ligtot)
ActiveCell.FormulaR1C1 =3D "=3DIF(RC[-9]<>0,RC[-1]/ABS(RC[-9]),"" "")"
Application.Goto Range("T" & ligtot)
ActiveCell.FormulaR1C1 =3D "=3DIF(RC[-11]<>0,RC[-12]/ABS(RC[-11]),""
"")"
Columns("R:S").Select
Selection.NumberFormat =3D "#,##0"
'Mise en Gras + gris=E9
Range("A" & ligtot, "T" & ligtot).Select
Selection.Font.Bold =3D True

1 réponse

Avatar
Misange
Bonjour

Oui bien sur

Tu peux surtout commencer par simplifier ++ ton code : il n'est pas du
tout besoin d'aller dans une cellule pour y écrire !

ceci
Application.Goto Range("H" & ligty)
ActiveCell.Formula = "=SUBTOTAL(9,H2:H" & ActiveCell.Row - 1 & ")"

s'écrit plus simplement
Range("H" & ligty).formula= "=SUBTOTAL(9,H2:H" & ActiveCell.Row - 1 & ")"

Ensuite
tu écris par exemple

sub mamacro1()
... traitement 1 à faire sur chacune des feuilles
end sub

sub mamacro2()
... traitement 2 à faire sur chacune des feuilles
end sub

sub mamacroprincipale()
For Each Sht In Application.Worksheets
if Left(Sht.Name, 6) = "REGION" then
mamacro1
mamacro2
end if
next sht
end sub

Misange migrateuse
XlWiki : Participez à un travail collaboratif sur excel !
http://xlwiki.free.fr/wiki
http://www.excelabo.net

a écrit :
Bonjour à tous,
dans la macro que j'ai effectuée ... (grace à l'aide de certains) j'ai
des instructions qui se répètent pour certaines feuilles.
y a t'il la possibilité de mettre ces lignes de codes dans une sorte
de sous programme ?
Le code suivant est à faire pour la feuille REGION 1 à REGION 8 par
exemple
Merci de vos réponses

Sheets("REGION 1").Select
'====================================================== > 'Totaux Y
nby = Application.WorksheetFunction.CountIf(Range("G1:G200"), "Y
")
ligty = nby + 2
debny = ligty + 2
Application.Goto Range("A" & ligty)
Range(ActiveCell, ActiveCell.Offset(1, 20)).Select
Selection.Insert Shift:=xlDown
Range("F" & ligty) = "TOTAL Y"
Application.Goto Range("H" & ligty)
ActiveCell.Formula = "=SUBTOTAL(9,H2:H" & ActiveCell.Row - 1 & ")"
Application.Goto Range("I" & ligty)
ActiveCell.Formula = "=SUBTOTAL(9,I2:I" & ActiveCell.Row - 1 & ")"
Application.Goto Range("J" & ligty)
ActiveCell.Formula = "=SUBTOTAL(9,J2:J" & ActiveCell.Row - 1 & ")"
Application.Goto Range("L" & ligty)
ActiveCell.Formula = "=SUBTOTAL(9,L2:L" & ActiveCell.Row - 1 & ")"
Application.Goto Range("N" & ligty)
ActiveCell.Formula = "=SUBTOTAL(9,N2:N" & ActiveCell.Row - 1 & ")"
Application.Goto Range("P" & ligty)
ActiveCell.Formula = "=SUBTOTAL(9,P2:P" & ActiveCell.Row - 1 & ")"
Application.Goto Range("R" & ligty)
ActiveCell.Formula = "=SUBTOTAL(9,R2:R" & ActiveCell.Row - 1 & ")"
Application.Goto Range("S" & ligty)
ActiveCell.Formula = "=SUBTOTAL(9,S2:S" & ActiveCell.Row - 1 & ")"
Application.Goto Range("K" & ligty)
ActiveCell.FormulaR1C1 = "=IF(RC[-3]<>0,RC[-1]/ABS(RC[-3]),"" "")"
Application.Goto Range("M" & ligty)
ActiveCell.FormulaR1C1 = "=IF(RC[-5]<>0,RC[-1]/ABS(RC[-5]),"" "")"
Application.Goto Range("O" & ligty)
ActiveCell.FormulaR1C1 = "=IF(RC[-7]<>0,RC[-1]/ABS(RC[-7]),"" "")"
Application.Goto Range("Q" & ligty)
ActiveCell.FormulaR1C1 = "=IF(RC[-9]<>0,RC[-1]/ABS(RC[-9]),"" "")"
Application.Goto Range("T" & ligty)
ActiveCell.FormulaR1C1 = "=IF(RC[-11]<>0,RC[-12]/ABS(RC[-11]),""
"")"
'Mise en Gras
Range("A" & ligty, "T" & ligty).Select
Selection.Font.Bold = True
'====================================================== > 'Totaux NY
nbny = Application.WorksheetFunction.CountIf(Range("G1:G200"),
"NY")
ligtny = nbny + 2 + ligty
ligtot = ligtny + 2
Application.Goto Range("A" & ligtny)
Range(ActiveCell, ActiveCell.Offset(1, 20)).Select
Selection.Insert Shift:=xlDown
Range("F" & ligtny) = "TOTAL NY"
Application.Goto Range("H" & ligtny)
ActiveCell.Formula = "=SUBTOTAL(9,H" & debny & ":H" &
ActiveCell.Row - 1 & ")"
Application.Goto Range("I" & ligtny)
ActiveCell.Formula = "=SUBTOTAL(9,I" & debny & ":I" &
ActiveCell.Row - 1 & ")"
Application.Goto Range("J" & ligtny)
ActiveCell.Formula = "=SUBTOTAL(9,J" & debny & ":J" &
ActiveCell.Row - 1 & ")"
Application.Goto Range("L" & ligtny)
ActiveCell.Formula = "=SUBTOTAL(9,L" & debny & ":L" &
ActiveCell.Row - 1 & ")"
Application.Goto Range("N" & ligtny)
ActiveCell.Formula = "=SUBTOTAL(9,N" & debny & ":N" &
ActiveCell.Row - 1 & ")"
Application.Goto Range("P" & ligtny)
ActiveCell.Formula = "=SUBTOTAL(9,P" & debny & ":P" &
ActiveCell.Row - 1 & ")"
Application.Goto Range("R" & ligtny)
ActiveCell.Formula = "=SUBTOTAL(9,R" & debny & ":R" &
ActiveCell.Row - 1 & ")"
Application.Goto Range("S" & ligtny)
ActiveCell.Formula = "=SUBTOTAL(9,S" & debny & ":S" &
ActiveCell.Row - 1 & ")"
Application.Goto Range("K" & ligtny)
ActiveCell.FormulaR1C1 = "=IF(RC[-3]<>0,RC[-1]/ABS(RC[-3]),"" "")"
Application.Goto Range("M" & ligtny)
ActiveCell.FormulaR1C1 = "=IF(RC[-5]<>0,RC[-1]/ABS(RC[-5]),"" "")"
Application.Goto Range("O" & ligtny)
ActiveCell.FormulaR1C1 = "=IF(RC[-7]<>0,RC[-1]/ABS(RC[-7]),"" "")"
Application.Goto Range("Q" & ligtny)
ActiveCell.FormulaR1C1 = "=IF(RC[-9]<>0,RC[-1]/ABS(RC[-9]),"" "")"
Application.Goto Range("T" & ligtny)
ActiveCell.FormulaR1C1 = "=IF(RC[-11]<>0,RC[-12]/ABS(RC[-11]),""
"")"
'Mise en gras
Range("A" & ligtny, "T" & ligtny).Select
Selection.Font.Bold = True
'====================================================== > 'Totaux Y + NY
Range("F" & ligtot) = "TOTAL REGION"
Range("H" & ligtot) = Range("H" & ligty) + Range("H" & ligtny)
Range("I" & ligtot) = Range("I" & ligty) + Range("I" & ligtny)
Range("J" & ligtot) = Range("J" & ligty) + Range("J" & ligtny)
Range("L" & ligtot) = Range("L" & ligty) + Range("L" & ligtny)
Range("N" & ligtot) = Range("N" & ligty) + Range("N" & ligtny)
Range("P" & ligtot) = Range("P" & ligty) + Range("P" & ligtny)
Range("R" & ligtot) = Range("R" & ligty) + Range("R" & ligtny)
Range("S" & ligtot) = Range("S" & ligty) + Range("S" & ligtny)
Application.Goto Range("K" & ligtot)
ActiveCell.FormulaR1C1 = "=IF(RC[-3]<>0,RC[-1]/ABS(RC[-3]),"" "")"
Application.Goto Range("M" & ligtot)
ActiveCell.FormulaR1C1 = "=IF(RC[-5]<>0,RC[-1]/ABS(RC[-5]),"" "")"
Application.Goto Range("O" & ligtot)
ActiveCell.FormulaR1C1 = "=IF(RC[-7]<>0,RC[-1]/ABS(RC[-7]),"" "")"
Application.Goto Range("Q" & ligtot)
ActiveCell.FormulaR1C1 = "=IF(RC[-9]<>0,RC[-1]/ABS(RC[-9]),"" "")"
Application.Goto Range("T" & ligtot)
ActiveCell.FormulaR1C1 = "=IF(RC[-11]<>0,RC[-12]/ABS(RC[-11]),""
"")"
Columns("R:S").Select
Selection.NumberFormat = "#,##0"
'Mise en Gras + grisé
Range("A" & ligtot, "T" & ligtot).Select
Selection.Font.Bold = True