Optimisation de macro grâce aux tableaux
Bonjour,
J'ai mis au point une macro qui me permet de sourcer, trier, et retraiter les données d'un classeur pour les importer dans un autre.
Problème : cette macro met un certain temps à s'effectuer. Elle pourrait selon moi être plus rapide grâce aux tableaux mais je ne maîtrise pas du tout cet objet.
Avez-vous une solution pour retranscrire cette macro afin d'optimiser le temps d'exécution ?
'TradeBase.xlsx
Dim wbBase As Workbook
Dim wsBase As Worksheet
Set wbBase = Workbooks.Open("Q:\Execution\Front_Middle\ACS\TradeBase.xlsx")
Set wsBase = wbBase.Worksheets("PMS extract")
Dim Lignes As Integer
Lignes = Cells(Rows.Count, 1).End(xlUp).Row
Dim g As Integer
Dim Fund As String, Status As String, Name As String, Direction As String, Ccy As String
Dim TDate As String, SDate As String, Amount As String, Qty As Long
For g = 2 To Lignes
If wsBase.Cells(g, 8).Value = "Deleted" Then Rows(g).EntireRow.Delete
If wsBase.Cells(g, 8).Value = "Market" Then Rows(g).EntireRow.Delete
If wsBase.Cells(g, 8).Value = "Edit" Then Rows(g).EntireRow.Delete
Next
Dim NewLignes As Integer
NewLignes = Cells(Rows.Count, 1).End(xlUp).Row
For g = 2 To NewLignes
Fund = wsBase.Cells(g, 5).Value
wsOrder.Cells(g, 1).Value = Fund
Name = wsBase.Cells(g, 10).Value
wsOrder.Cells(g, 3).Value = Name
TDate = wsBase.Cells(g, 9).Value
wsOrder.Cells(g, 9).Value = TDate
Direction = wsBase.Cells(g, 13).Value
wsOrder.Cells(g, 2).Value = Direction
Qty = wsBase.Cells(g, 14).Value
wsOrder.Cells(g, 4).Value = Qty
Ccy = wsBase.Cells(g, 16).Value
wsOrder.Cells(g, 7).Value = Ccy
SDate = wsBase.Cells(g, 17).Value
wsOrder.Cells(g, 10).Value = SDate
Amount = wsBase.Cells(g, 18).Value
wsOrder.Cells(g, 5).Value = Left(Amount, Len(Amount) - 4)
Next
wbBase.Close
'import données trades en cours de settlement
Dim LignesACompter As Integer, h As Integer
LignesACompter = Application.WorksheetFunction.CountA(wsOrder.Range("$A:$A"))
Dim MontantX As Long, MontantY As Long, MontantZ As Long, MontantW As Long
MontantX = 0
MontantY = 0
MontantZ = 0
MontantW = 0
For h = 2 To LignesACompter
If wsOrder.Cells(h, 1).Value = "XXX" Then
MontantX = MontantX + wsOrder.Cells(h, 13).Value
End If
If wsOrder.Cells(h, 1).Value = "YYY" Then
MontantY = MontantY + wsOrder.Cells(h, 13).Value
End If
If wsOrder.Cells(h, 1).Value = "ZZZ" Then
MontantZ = MontantZ + wsOrder.Cells(h, 13).Value
End If
If wsOrder.Cells(h, 1).Value = "WWW" Then
MontantW = MontantW + wsOrder.Cells(h, 13).Value
End If
Next
wsCash.Cells(5, 4).Value = wsCash.Cells(5, 3).Value + MontantX
wsCash.Cells(6, 4).Value = wsCash.Cells(6, 3).Value + MontantY
wsCash.Cells(7, 4).Value = wsCash.Cells(7, 3).Value + MontantZ
wsCash.Cells(8, 4).Value = wsCash.Cells(8, 3).Value + MontantW
Par avance merci
Hello,
Elle pourrait selon moi être plus rapide grâce aux tableaux mais je ne maîtrise pas du tout cet objet
Avant de te lancer dans l'inconnu, peut être essayer ceci :
Application.ScreenUpdating = false
Application.Calculation = xlManual
'Ton code
Application.ScreenUpdating = True
Application.Calculation = xlAutomaticBonjour !
Je ne connaissais pas du tout cette propriété ScreenUpdating ; en effet l'exécution du code est beaucoup plus rapide, très utile !
Merci pour ton aide, je pense que ça me servira dans de nombreuses autres macro ;)
Hello,
Application.ScreenUpdating = falseDésactive l'actualisation de l'ecran.
Et
Application.Calculation = xlManualEvite le recalcul des formules du classeur.