I have a sheet with a lot of data (almost 14.000 rows and 13 columns).
I am running a For
loop within this sheet but it takes sometimes over 2 minutes to complete it. Also the application is not responding during the For
loop.
Is there a way I can re-write my loop so it will run a lot faster?
Here is my code:
For counter = 1 To Rows.Count
If Cells(counter, 13).Value > 500 Then
Cells(counter, 13).Interior.ColorIndex = 37
Cells(counter, 13).Font.Color = Black
Cells(counter, 13).Font.Bold = True
End If
count = count + 1
Application.StatusBar = count
Next counter
Thanks in advance :).
Avoid looping through a range. You can speed up your code by looping through an array and do formatting after it. Furthermore you could split your loop for the status bar count into portions.
Code
Rows.Count
includes every row, not just the ones with data. (1,048,576 rows in Excel 2016). The status bar shouldn't slow it down too much.The reason your code slows down is it takes all the rows when you're writing Rows.Count.
Try to limit your range and update the format at once at the very end which should fix your problem.
Below code takes 50000 cells and completes in more or less 8 seconds on my machine.
I also tried for each loop with almost same times.