MACRO EXCEL fonctionne mais lente...
Résolu
BipBip
-
Le Pingou Messages postés 12249 Date d'inscription Statut Contributeur Dernière intervention -
Le Pingou Messages postés 12249 Date d'inscription Statut Contributeur Dernière intervention -
Bonjour à tous,
J'ai fait ce code (voir plus bas) pour filtrer en même temps des Tableaux Croisés Dynamiques, en fonction du contenu de la cellule A1.
Cela fonctionne très bien sauf que le code est assez long = environ 1 minute 30
Auriez vous une idée pour accélerer un peu la procédure?
Merci à ceux qui m'aideront car c'est assez urgent (comme toujours!)
Bien à vous
BipBip
Sub Macro1()
Dim monPivIt As Object, Mavariable
Mavariable = Worksheets("Graphs").Range("A1").Value
Application.ScreenUpdating = False
With ActiveSheet.PivotTables("Tableau croisé dynamique1").PivotFields("FDR")
For Each monPivIt In .PivotItems
monPivIt.Visible = True
On Error Resume Next
Next
For Each monPivIt In .PivotItems
If monPivIt.Name <> Mavariable Then monPivIt.Visible = False
Next
End With
With ActiveSheet.PivotTables("Tableau croisé dynamique2").PivotFields("FDR")
For Each monPivIt In .PivotItems
monPivIt.Visible = True
On Error Resume Next
Next
For Each monPivIt In .PivotItems
If monPivIt.Name <> Mavariable Then monPivIt.Visible = False
Next
End With
With ActiveSheet.PivotTables("Tableau croisé dynamique3").PivotFields("FDR")
For Each monPivIt In .PivotItems
monPivIt.Visible = True
On Error Resume Next
Next
For Each monPivIt In .PivotItems
If monPivIt.Name <> Mavariable Then monPivIt.Visible = False
Next
End With
With ActiveSheet.PivotTables("Tableau croisé dynamique4").PivotFields("FDR")
For Each monPivIt In .PivotItems
monPivIt.Visible = True
On Error Resume Next
Next
For Each monPivIt In .PivotItems
If monPivIt.Name <> Mavariable Then monPivIt.Visible = False
Next
End With
Application.ScreenUpdating = True
With ActiveSheet.PivotTables("Tableau croisé dynamique5").PivotFields("FDR")
For Each monPivIt In .PivotItems
monPivIt.Visible = True
On Error Resume Next
Next
For Each monPivIt In .PivotItems
If monPivIt.Name <> Mavariable Then monPivIt.Visible = False
Next
End With
With ActiveSheet.PivotTables("Tableau croisé dynamique6").PivotFields("FDR")
For Each monPivIt In .PivotItems
monPivIt.Visible = True
On Error Resume Next
Next
For Each monPivIt In .PivotItems
If monPivIt.Name <> Mavariable Then monPivIt.Visible = False
Next
End With
With ActiveSheet.PivotTables("Tableau croisé dynamique7").PivotFields("FDR")
For Each monPivIt In .PivotItems
monPivIt.Visible = True
On Error Resume Next
Next
For Each monPivIt In .PivotItems
If monPivIt.Name <> Mavariable Then monPivIt.Visible = False
Next
End With
End Sub
J'ai fait ce code (voir plus bas) pour filtrer en même temps des Tableaux Croisés Dynamiques, en fonction du contenu de la cellule A1.
Cela fonctionne très bien sauf que le code est assez long = environ 1 minute 30
Auriez vous une idée pour accélerer un peu la procédure?
Merci à ceux qui m'aideront car c'est assez urgent (comme toujours!)
Bien à vous
BipBip
Sub Macro1()
Dim monPivIt As Object, Mavariable
Mavariable = Worksheets("Graphs").Range("A1").Value
Application.ScreenUpdating = False
With ActiveSheet.PivotTables("Tableau croisé dynamique1").PivotFields("FDR")
For Each monPivIt In .PivotItems
monPivIt.Visible = True
On Error Resume Next
Next
For Each monPivIt In .PivotItems
If monPivIt.Name <> Mavariable Then monPivIt.Visible = False
Next
End With
With ActiveSheet.PivotTables("Tableau croisé dynamique2").PivotFields("FDR")
For Each monPivIt In .PivotItems
monPivIt.Visible = True
On Error Resume Next
Next
For Each monPivIt In .PivotItems
If monPivIt.Name <> Mavariable Then monPivIt.Visible = False
Next
End With
With ActiveSheet.PivotTables("Tableau croisé dynamique3").PivotFields("FDR")
For Each monPivIt In .PivotItems
monPivIt.Visible = True
On Error Resume Next
Next
For Each monPivIt In .PivotItems
If monPivIt.Name <> Mavariable Then monPivIt.Visible = False
Next
End With
With ActiveSheet.PivotTables("Tableau croisé dynamique4").PivotFields("FDR")
For Each monPivIt In .PivotItems
monPivIt.Visible = True
On Error Resume Next
Next
For Each monPivIt In .PivotItems
If monPivIt.Name <> Mavariable Then monPivIt.Visible = False
Next
End With
Application.ScreenUpdating = True
With ActiveSheet.PivotTables("Tableau croisé dynamique5").PivotFields("FDR")
For Each monPivIt In .PivotItems
monPivIt.Visible = True
On Error Resume Next
Next
For Each monPivIt In .PivotItems
If monPivIt.Name <> Mavariable Then monPivIt.Visible = False
Next
End With
With ActiveSheet.PivotTables("Tableau croisé dynamique6").PivotFields("FDR")
For Each monPivIt In .PivotItems
monPivIt.Visible = True
On Error Resume Next
Next
For Each monPivIt In .PivotItems
If monPivIt.Name <> Mavariable Then monPivIt.Visible = False
Next
End With
With ActiveSheet.PivotTables("Tableau croisé dynamique7").PivotFields("FDR")
For Each monPivIt In .PivotItems
monPivIt.Visible = True
On Error Resume Next
Next
For Each monPivIt In .PivotItems
If monPivIt.Name <> Mavariable Then monPivIt.Visible = False
Next
End With
End Sub
A voir également:
- MACRO EXCEL fonctionne mais lente...
- Telecharger macro convertir chiffre en lettre excel - Télécharger - Tableur
- Liste déroulante excel - Guide
- Word et excel gratuit - Guide
- Déplacer colonne excel - Guide
- Si ou excel - Guide
7 réponses
Bonjour BipBip,
Je vous propose d'essayer cette procédure :
Je vous propose d'essayer cette procédure :
Sub FiltrerTdc() Dim monPivIt As Object, Mavariable Dim nbtdc As Long Mavariable = Worksheets("Graphs").Range("A1").Value nbtdc = ActiveSheet.PivotTables.Count Application.ScreenUpdating = False For c = 1 To nbtdc With ActiveSheet.PivotTables(c).PivotFields("FDR") .PivotItems(Mavariable).Visible = True For Each monPivIt In .PivotItems If monPivIt.Name <> Mavariable Then monPivIt.Visible = False Next End With Next c Application.ScreenUpdating = True End SubMerci de vos commentaires.
Bonjour,
Juste au passage, pourquoi 7 fois 2 boucles identiques, une devrait suffire !
Salutations.
Le Pingou
Juste au passage, pourquoi 7 fois 2 boucles identiques, une devrait suffire !
Sub Macro1() Dim monPivIt As Object, Mavariable Mavariable = Worksheets("Graphs").Range("A1").Value Application.ScreenUpdating = False With ActiveSheet.PivotTables("Tableau croisé dynamique1").PivotFields("FDR") For Each monPivIt In .PivotItems monPivIt.Visible = True On Error Resume Next Next For Each monPivIt In .PivotItems If monPivIt.Name <> Mavariable Then monPivIt.Visible = False Next End With End Sub--
Salutations.
Le Pingou
Bonjour,
En complément de la réponse ci-dessus privilégiez, si possible, une structure en Do While plutôt qu'un For Each Next.
Le Do While ayant l'avantage d'éviter qu'Excel ne balaye tous les enregistrements et est donc plus performant qu'un For Each qui parcourera systématiquement tous les enregistrements.
Bien à vous
En complément de la réponse ci-dessus privilégiez, si possible, une structure en Do While plutôt qu'un For Each Next.
Le Do While ayant l'avantage d'éviter qu'Excel ne balaye tous les enregistrements et est donc plus performant qu'un For Each qui parcourera systématiquement tous les enregistrements.
Bien à vous
Bonjour,
Essayez de remplacer cette partie :
Salutations.
Le Pingou
Essayez de remplacer cette partie :
For Each monPivIt In .PivotItems monPivIt.Visible = True On Error Resume Next Next For Each monPivIt In .PivotItems If monPivIt.Name <> Mavariable Then monPivIt.Visible = False NextPar :
.PivotItems(Mavariable).Visible = True 'For Each monPivIt In .PivotItems 'monPivIt.Visible = True 'On Error Resume Next 'Next For Each monPivIt In .PivotItems If monPivIt.Name <> Mavariable Then monPivIt.Visible = False Next
Salutations.
Le Pingou
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question
Bonjour Pingou,
Merci beaucoup pour ta réponse. Depuis mon dernier message, j'ai modifié en effet cette partie du code qui me semblait inutile. Il me semble que cette version est proche de la tienne (voir ci-dessous). Qu'en penses-tu?
With Worksheets("TCD").PivotTables("Tableau croisé dynamique1").PivotFields("FDR")
.ClearAllFilters
On Error Resume Next
For Each monPivIt In .PivotItems
If monPivIt.Name <> Mavariable Then monPivIt.Visible = False
Next
End With
@Syzygy : bonjour, Je ne sais pas faire de DoWhile... mais suis preneur si tu penses que c'est une solution plus "légère", car ma macro est relativement longue (2 minutes) et je vois les graphs liés aux TCD "s'affoler" en direct sur mon écran...
Merci beaucoup pour ta réponse. Depuis mon dernier message, j'ai modifié en effet cette partie du code qui me semblait inutile. Il me semble que cette version est proche de la tienne (voir ci-dessous). Qu'en penses-tu?
With Worksheets("TCD").PivotTables("Tableau croisé dynamique1").PivotFields("FDR")
.ClearAllFilters
On Error Resume Next
For Each monPivIt In .PivotItems
If monPivIt.Name <> Mavariable Then monPivIt.Visible = False
Next
End With
@Syzygy : bonjour, Je ne sais pas faire de DoWhile... mais suis preneur si tu penses que c'est une solution plus "légère", car ma macro est relativement longue (2 minutes) et je vois les graphs liés aux TCD "s'affoler" en direct sur mon écran...
Bonjour,
Je ne fais que passer...
je vois les graphs liés aux TCD "s'affoler" en direct sur mon écran...
Fais comme Le Pingou l'a indiqué dans le code decette réponse:
Je ne fais que passer...
je vois les graphs liés aux TCD "s'affoler" en direct sur mon écran...
Fais comme Le Pingou l'a indiqué dans le code decette réponse:
Application.ScreenUpdating = False
Bonjour,
de passage aussi
tes graphes sont indépendants (ce sont une sorte d'image améliorée) de la feuille et screenupdating ne travaille que sur la feuille
regarde si tu as la propriété visible avec les charts (j'en sais rien)
de passage aussi
tes graphes sont indépendants (ce sont une sorte d'image améliorée) de la feuille et screenupdating ne travaille que sur la feuille
regarde si tu as la propriété visible avec les charts (j'en sais rien)
Je suis bluffé... avec votre code Pingou, je passe de 2 minutes à 2 secondes... et en bonus : plus rien ne clignote...
Un GRAND merci pour votre aide, vous balayez d'un revers de main quelques heures de prise de tête...
Encore merci Pingou pour votre aide (et le suivi)
Si je peux faire quelque chose pour vous...
Salutations
Olivier
Ca marche, c'est parfait.
Le merci me suffit.
Salutations.
Le Pingou