Problème sur une boucle For Each
Bonjour à tous,
Je suis entrain de faire une macro qui doit enregistrer un fichier à un autre endroit sous un nom différent, supprimer les onglets qui ont la cellule A2 vide, et ensuite appliquer une certaine mise en page sur chaque onglet. Je suis passée par une boucle For Each. Le code fonctionne très bien lorsque le fichier ne contient qu'un seul onglet, mais dès qu'il y en a plusieurs, le code fonctionne sur le premier onglet mais au lieu de passer à l'onglet d'après il refait une boucle de mise en page sur le même onglet.
Je vous laisse mon code pour que vous puissiez regarder où je me suis trompée.
Merci beaucoup
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
| Workbooks.Open Filename:=ChSFI
ActiveWorkbook.RefreshAll
'Enregistre une copie du fichier SFI avec la date du mensuel
date_analyse = Range("A2")
date_analyse = Format(date_analyse, "yyyy mm dd")
nom_fichier = date_analyse & " Mensuel SFI.xlsm"
If Fichierexiste(cheminsave & nom_fichier) Then 'on appelle la fonction fichierexiste
reponse = MsgBox("Attention le fichier " & nom_fichier & " existe deja, voulez vous le remplacer", vbYesNo)
If reponse = vbYes Then
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs Filename:= _
cheminsave & nom_fichier
Else
Application.Dialogs(xlDialogSaveAs).Show "Copie de " & cheminsave & nom_fichier
End If
Else ' si le fichier exsite pas fileexist = false
ActiveWorkbook.SaveAs Filename:= _
cheminsave & nom_fichier
End If
'Supprime les onglets qui sont vides
Application.DisplayAlerts = False
For Each WS In Sheets
NbOnglet = ActiveWorkbook.Sheets.Count
If NbOnglet > 1 Then
If Range("B4") = "" Then
WS.Delete
Else
End If
Else
End If
'Ajoute la colonne "A:A"
Columns("A:A").Select
Range("A1").Activate
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
'Groupement des colonnes
Columns("C:C").Select
Selection.Columns.Group
Columns("G:H").Select
Selection.Columns.Group
Columns("M:M").Select
Selection.Columns.Group
Columns("O:O").Select
Selection.Columns.Group
Columns("R:R").Select
Selection.Columns.Group
Columns("AB:AF").Select
Selection.Columns.Group
Columns("AM:AP").Select
Selection.Columns.Group
Columns("AR:AY").Select
Selection.Columns.Group
'Ajoute la ligne Client Identification
Rows("1:2").Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Range("B2:F2").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Merge
Range("G2:L2").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Merge
ActiveCell.FormulaR1C1 = "Client Identification"
Range("M2:AE2").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Merge
ActiveCell.FormulaR1C1 = "Anomaly Description"
Range("AF2:AP2").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Merge
ActiveCell.FormulaR1C1 = "Anomaly Analysis"
Range("AQ2:BB2").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Merge
ActiveCell.FormulaR1C1 = "Additional Information"
'Format aux couleurs CACIB de l'entete
Range("B2:BB3").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 5733632
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With Selection.Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With
ActiveSheet.Outline.ShowLevels RowLevels:=0, ColumnLevels:=1
Next |