Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 15521

How can I speed up my VBA code running inside a For loop through a lot of lines

$
0
0

I hope you are well.

I'm running a For loop through a big Excel sheet (arround 4000 lines), to create a new table inside another sheet.

The loop goes through every lines, check if the cliend ID already exist in the new table :

  • If yes, it goes to the line, insert a new line with all the informations
  • If no, it goes to the end of the table, and insert a new line with all the informations

Here is the code :

For ligneRawData = 2 To derniereLigneRawData' Searching if client already exist    Set celluleTrouvee = Worksheets("Tableau").Columns("O").Find(What:=Worksheets("RawData").Cells(ligneRawData, "A").Value, LookIn:=xlValues, LookAt:=xlWhole)    If Not celluleTrouvee Is Nothing Then'client already exist        clientExiste = True    Else'client not existing, we have to create it        derniereLigneActuelleTableau = Worksheets("Tableau").Cells(Worksheets("Tableau").Rows.Count, "C").End(xlUp).Row + 1 ' going to last line' Adding client        Worksheets("Tableau").Cells(derniereLigneActuelleTableau, "C").Value = Worksheets("RawData").Cells(ligneRawData, "B").Value ' Cient name        Worksheets("Tableau").Cells(derniereLigneActuelleTableau, "D").Value = Worksheets("RawData").Cells(ligneRawData, "I").Value ' Leader name        Worksheets("Tableau").Cells(derniereLigneActuelleTableau, "O").Value = Worksheets("RawData").Cells(ligneRawData, "A").Value ' Client ID    End If

' CODE NEED 5 seconds to execute before that :' CODE NEED 500 seconds to execute the statement below :

' Storing data in a array to prevent multiples query to the other sheet    values = Worksheets("RawData").Range("C" & ligneRawData & ":O" & ligneRawData).Value2    If clientExiste Then        clientLigne = celluleTrouvee.Row + 1        Worksheets("Tableau").Rows(clientLigne).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove    Else        clientLigne = derniereLigneActuelleTableau + 1        Worksheets("Tableau").Rows(clientLigne).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove    End If' Storing data in the new table    Worksheets("Tableau").Cells(clientLigne, "C").Value = values(1, 3) & " / " & values(1, 4)    Worksheets("Tableau").Cells(clientLigne, "E").Value = values(1, 9)    Worksheets("Tableau").Cells(clientLigne, "F").Value = values(1, 11)    Worksheets("Tableau").Cells(clientLigne, "G").Value = values(1, 10)    Worksheets("Tableau").Cells(clientLigne, "H").Value = values(1, 12)    Worksheets("Tableau").Cells(clientLigne, "I").Value = values(1, 13)    clientExiste = FalseNext ligneRawData

Did anyone know how to speed up my VBA code ?Big thanks in advance


Viewing all articles
Browse latest Browse all 15521

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>