Loop using range contains variable

tttommy2

Board Regular
Joined
Oct 1, 2012
Messages
55
I have a sheet with a large range which is broken down to several thousand boxes of 3 cells. I want to format the boxes based on the last cell value of each box. Conditional Format is too messy and also I may need to change the way I format each box.

I have an idea with VBA but can't get it to work
VBA Code:
Dim Box1 As Range, Box2 As Range, Box3 As Range, Box4 As Range
Dim XXX As Range
Dim i As long

Set Box1 = Range(“A1:A3”)
Set Box2 = Range(“A4:A6”)
Set Box3 = Range(“A7:A9”)
Set Box4 = Range(“A10:A12”)

For i = 1 to 4
Set XXX = "Box" & “i"
If XXX(3) > 0 Then XXX.Font.ColorIndex = 5
Next i

In the attached code I have simplified the code down to 4 Boxes from 1200 and the the formatting to setting the font color.

I am trying to get it to loop thru Range XXX = Box1, Box2, Box3, Box4, etc.

Any ideas?

Thank you
 

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
This is the useless way as if the worksheet is enough smart designed it should be achieved directly without harcoding anything !​
Anyway another hardcode way without any useless Range variable :​
VBA Code:
         Dim V
    For Each V In [{"A1:A3","A4:A6","A7:A9","A10:A12"}]
        With Range(V)
            If .Cells(3).Value2 > 0 Then .Font.ColorIndex = 5
        End With
    Next
 
Upvote 0
An easy way :​
VBA Code:
    Dim R&
   
    For R = 3 To 12 Step 3
        If Cells(R, 1).Value2 > 0 Then Range(Cells(R - 2, 1), Cells(R, 1)).Font.ColorIndex = 5
    Next
 
Upvote 0
Solution
Thank you Marc L. Your "easy way" is very nice and efficient way to solve my issue.


VBA Code:
    Dim R&
   
    For R = 3 To 12 Step 3
        If Cells(R, 1).Value2 > 0 Then Range(Cells(R - 2, 1), Cells(R, 1)).Font.ColorIndex = 5
    Next
 
Upvote 0

Forum statistics

Threads
1,214,954
Messages
6,122,462
Members
449,085
Latest member
ExcelError

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