VBA to cycle through range

rjplante

Well-known Member
Joined
Oct 31, 2008
Messages
569
Office Version
  1. 365
Platform
  1. Windows
I have a range of cells that spans a block of columns, but there is a blank column in between each of the columns of data. I want to start at the top left most cell and have it go down to the bottom of that column and the over to the right to the next block of cells 2 columns over. Then down and over until it has reached the last cell in the block. The goal is to find the first cell that does not have any color in it and then apply the desired color to that cell and then exit the sub. My code is listed below and when I run it, it moves to the first cell in the range and then displays "Done" indicating that the macro has run, but nothing happens. No cells had their interior color changed. What am I doing wrong and how do I fix it?


VBA Code:
Sub COLOR_ME()

Dim Cell As Range

For Each Cell In Range("AC4:AC7,AE4:AE7,AG4:AG7,AI4:AI7,AK4:AK7")
    If Cell.Interior.Color = xlNone Then
        ActiveCell.Interior.Color = RGB(75, 205, 255)
        Exit Sub
    End if
Next Cell

MsgBox "Done"

End Sub
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
You need to change this line:
VBA Code:
ActiveCell.Interior.Color = RGB(75, 205, 255)
to this:
VBA Code:
Cell.Interior.Color = RGB(75, 205, 255)
as the ActiveCell never changes in your code, as you are not selecting any cells.
 
Upvote 0
Thanks for that change. I have made the correction, but there is still no change in any of the cells when I run the macro. No color has been added. Any other thoughts?
 
Upvote 0
Thanks for that change. I have made the correction, but there is still no change in any of the cells when I run the macro. No color has been added. Any other thoughts?
On testing, it also looks like this:
VBA Code:
    If Cell.Interior.Color = xlNone Then
needs to change to this:
VBA Code:
    If Cell.Interior.Pattern = xlNone Then

So your final code should look like:
VBA Code:
Sub COLOR_ME()

Dim Cell As Range

For Each Cell In Range("AC4:AC7,AE4:AE7,AG4:AG7,AI4:AI7,AK4:AK7")
    If Cell.Interior.Pattern = xlNone Then
        Cell.Interior.Color = RGB(75, 205, 255)
        Exit Sub
    End If
Next Cell

MsgBox "Done"

End Sub
 
Upvote 0
Solution
That did it!! Thanks for the interior color education. I appreciate your quick response.
 
Upvote 0
You are welcome.
Glad I was able to help!

When in doubt, just turn on the Macro Recorder and record yourself adding and removing color from cells, and that helps you see what Excel is expecting.
 
Upvote 0
Forgot about that simple solution option. Thanks again.
 
Upvote 0

Forum statistics

Threads
1,215,084
Messages
6,123,024
Members
449,092
Latest member
ikke

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