Cell.Offset(, ).Interior.ColorIndex Very Slow

JStellato

Board Regular
Joined
Nov 6, 2010
Messages
55
Using this code, the result of the debug.print are between 500 - 600 milliseconds. That's a half a second. I am running a loop of 2000 cells, so that adds up very quickly.

Is there any other way to speed this up? For reference I have set these options first:


Code:
    Application.ScreenUpdating = False
    Application.StatusBar = False
    Application.Calculation = xlCalculationManual
    Application.DisplayStatusBar = False
    Application.EnableEvents = False

I'm timing it using the tickcount

Code:
Public Declare Function GetTickCount Lib "kernel32.dll" () As Long

t = GetTickCount
    Cell.Offset(, 1).Interior.ColorIndex = 4 ' This is the line that is taking 500ms
Debug.Print GetTickCount - t & " Setting Color Index = 4"
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Using this code, the result of the debug.print are between 500 - 600 milliseconds. That's a half a second. I am running a loop of 2000 cells, so that adds up very quickly.

Is there any other way to speed this up?
We cannot tell you if you can speed anything up until we see the code you are using, so... post your current code.
 
Upvote 0
Sorry in my haste I forgot to copy that:
Code:
Dim dataRange As Range
Set dataRange = Range(Cells(1, Col), Cells(LastRow1, Col))

For Each Cell In dataRange
    t = GetTickCount
    Cell.Offset(, 1).Interior.ColorIndex = 4 ' This is the line that is taking 500ms
    Debug.Print GetTickCount - t & " Setting Color Index = 4"
Next Cell
 
Upvote 0
Sorry in my haste I forgot to copy that:
Code:
Dim dataRange As Range
Set dataRange = Range(Cells(1, Col), Cells(LastRow1, Col))

For Each Cell In dataRange
    t = GetTickCount
    Cell.Offset(, 1).Interior.ColorIndex = 4 ' This is the line that is taking 500ms
    Debug.Print GetTickCount - t & " Setting Color Index = 4"
Next Cell
Well, of course, that is not all your code since you have references to variables Col and LastRow1 but show no assignment to them. However, if your code is representative of what you are actually doing (assigning the same ColorIndex value to all the cells, then you can speed things up dramatically by doing all the assignment all at once (no loop) using this single line of code (it assumes Col and LastRow1 were already assigned values)...

Code:
Range(Cells(1, Col), Cells(LastRow1, Col)).ColorIndex = 4
 
Upvote 0
Well, of course, that is not all your code since you have references to variables Col and LastRow1 but show no assignment to them. ]

It is not all of the code, I find when I break the problem down to the simplest form I get the most help.

My fault, as it's late at night and I'm not representing my problem correctly. But no the range option wouldn't work because contained are multiple if statements:



Code:
Dim dataRange As Range
Set dataRange = Range(Cells(1, 1), Cells(2000, 1))

For Each Cell In dataRange
    t = GetTickCount

   If foo < 10 then
         Cell.Offset(, 1).Interior.ColorIndex = 4 ' This is the line that is taking 500ms
   end if

   if foo > 10 then
         Cell.Offset(, 1).Interior.ColorIndex = 5 ' This is the line that is taking 500ms
   end if
    Debug.Print GetTickCount - t & " Setting Color Index "
Next Cell


Hopefully this clarifies it. Thanks for your help.
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top