Vitesse d'exécution d'une boucle

Rachel_01

XLDnaute Nouveau
Bonjour,

j'ai réussi a créer une macro qui récupère des données dans les nombreux onglets qui constituent mon fichier excel...

Etant débutante an VBA je pense que ma procédure n'est pas optimisée car je trouve qu'elle prend un peu trop de temps. Est-il possible d'accélérer le processus?

Sub compilation()
Dim sh As Worksheet
Dim Dercol As Long
Dim Lig As Long, Col As Integer
Lig = 1 'Première ligne où copier
Col = 1 'Colonne où copier
L = 2
Application.DisplayAlerts = False
Sheets("Compilation").Range("A1:FZ100").Clear



For Each sh In ActiveWorkbook.Sheets
Dim N As String
N = sh.Name

If (N <> "data") And (N <> "Indicateur") And (N <> Compilation) And (N <> Aide) Then
Sheets("Compilation").Cells(Lig, Col) = sh.Range("D1")
Sheets("Compilation").Cells(L, Col) = sh.Range("D8")
Col = Col + 2
End If
N = sh.Name

If (N <> "data") And (N <> "Indicateur") And (N <> Compilation) And (N <> Aide) Then
Dercol = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Column
sh.Range("B12:B50").Copy Sheets("Compilation").Cells(3, Dercol)
End If

N = sh.Name
If (N <> "data") And (N <> "Indicateur") And (N <> Compilation) And (N <> Aide) Then
Dercol = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Column
sh.Range("F12:F50").Copy Sheets("Compilation").Cells(3, Dercol + 1)
End If
Next
Sheets("Indicateur").Select
End Sub


Merci
 

gwenlorin

XLDnaute Occasionnel
Re : Vitesse d'exécution d'une boucle

Bonjour Rachel_01,

J'ai simplifié un peu ton code.

Code:
Sub compilation()
  Dim sh As Worksheet
  Dim Dercol As Long
  Dim Lig As Long, Col As Integer
  Lig = 1 'Première ligne où copier
Col = 1 'Colonne où copier
L = 2

Application.DisplayAlerts = False
Application.screenupdating = False '--- bloque mise à jour affichage gain rapidité exécution macro


Sheets("Compilation").Range("A1:FZ100").Clear

For Each sh In ActiveWorkbook.Sheets

If (sh.Name <> "data") And (sh.Name <> "Indicateur") And (sh.Name <> "Compilation") And (sh.Name <> "Aide") Then
     Sheets("Compilation").Cells(Lig, Col) = sh.Range("D1")
     Sheets("Compilation").Cells(L, Col) = sh.Range("D8")
     Col = Col + 2
     sh.Range("B12:B50").Copy Sheets("Compilation").Cells(3, ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Column)
     sh.Range("F12:F50").Copy Sheets("Compilation").Cells(3, ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Column + 1)
End If

next 

Sheets("Indicateur").Select

Application.DisplayAlerts = True
Application.screenupdating = True
End Sub
 
Dernière édition:

CBernardT

XLDnaute Barbatruc
Re : Vitesse d'exécution d'une boucle

Bonjour Rachel_01, gwenlorin et le forum,

Un essai de syntaxe :

Sub compilation()
Dim Ws As Worksheet, N As String, DerCol As Long, Lig As Long, Col As Integer
Lig = 1 'Première ligne où copier
Col = 1 'Colonne où copier
L = 2
Application.ScreenUpdating = False ' Désactivation de l'affichage afin d'accélérer le traitement
Application.Calculation = xlManual
Sheets("Compilation").Range("A1:FZ100").Clear
For Each Ws In Worksheets
N = Ws.Name
If (N <> "data") And (N <> "Indicateur") And (N <> compilation) And (N <> Aide) Then
With Sheets("Compilation")
.Cells(Lig, Col).Value = Ws.Range("D1").Value
.Cells(L, Col).Value = Ws.Range("D8").Value
DerCol = .UsedRange.SpecialCells(xlCellTypeLastCell).Column
Ws.Range("B12:B50").Copy .Cells(3, DerCol)
DerCol = .UsedRange.SpecialCells(xlCellTypeLastCell).Column
Ws.Range("F12:F50").Copy .Cells(3, DerCol + 1)
Col = Col + 2
End With
End If
Next
Application.ScreenUpdating = True
Sheets("Indicateur").Select
Application.Calculation = xlAutomatic
End Sub

Cordialement

Bernard
 

Discussions similaires

Statistiques des forums

Discussions
312 499
Messages
2 089 000
Membres
104 002
dernier inscrit
SkrauzTTV