Stepping thru a range in a sub

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,691
Office Version
  1. 365
Platform
  1. Windows
I need to step thru a range of cells in a Sub setting the fill to either red or green depending on whether the contents are less than or greater than 0.

Using the macro recorder and information from MSDN, I came up with this:

Code:
Const Col1 As Long = 3
Const Col2 As Long = 6
Const Row As Long = 22
Dim Col As Long
For Col = Col1 To Col2
  Range(.Cells(Row, Col), .Cells(Row, Col)).Select
  With Selection.Interior
    .Pattern = xlSolid
    .PatternColorIndex = xlAutomatic
    If .Value > 0 Then
      .color = 65280
    Else
      .color = 255
    End If
    .TintAndShade = 0
    .PatternTintAndShade = 0
  End With
Next Col

It fails on the Range statement with "Invalid or unqualified reference".

If I change the Range statement to

Code:
Range("C22:F22").Select

Then it fails on the If .value statement with "Object doesn't support this property or method".

Can someone help me with the correct syntax to get this to work?
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
To loop through all the cells in a range, you can do it like this:
Code:
Dim cell as Range
For each cell in Range("C22:F22")
    ...
Next cell
However, it seems like you could do what you want with Conditional Formatting. Is there a reason that you do not want to use it here?
 
Upvote 0
To loop through all the cells in a range, you can do it like this:
Code:
Dim cell as Range
For each cell in Range("C22:F22")
    ...
Next cell

OK, but how do I reference each cell inside the loop? I need to test each cell to see if it's greater or less than zero and then set the fill color accordingly. Can you provide the syntax for the test and then the color assignment?

However, it seems like you could do what you want with Conditional Formatting. Is there a reason that you do not want to use it here?

I was using conditional formatting, but now I need to copy the colored line to create a history. But when the conditions change, the rules no longer work properly. I couldn't find a way to copy the colors without the rules, so now I'm going to apply the colors in a macro so they can be copied.
 
Upvote 0
OK, but how do I reference each cell inside the loop?
With the name of the range variable we chose ("cell"), i.e.
Code:
If cell.Value > 0 Then
 
Upvote 0
With the name of the range variable we chose ("cell"), i.e.
Code:
If cell.Value > 0 Then

This seems to work:

Code:
Dim cell As Range
For Each cell In Range("I3:AA3")
  Select Case cell.Value
    Case Is > 0: cell.Interior.color = 65280
    Case Is < 0: cell.Interior.color = 255
  End Select
Next cell

Any comments?
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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