Loop through code and then run if statement

123excel

New Member
Joined
Jan 18, 2017
Messages
34
Hi, I am trying to loop through all cells in the range J10:J25. When a cell is smaller than the value in cell "D12", I want it to change color to red.

This is probably simple, but I am unsure how I can target each cell to check for the if function. ActiveCell does not seem to work.

I could not manage to find the answear on google, so I am very thankful for your help!

Here is the code I have come up with so far:

Code:
Sheets("Data").Select
Range("D12").Select


Dim rng As Range, cell As Range
Set rng = Range("J10:J25")
For Each cell In rng



If ActiveCell < Range("D12") Then


ActiveCell.Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 255
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
    End If


Next cell


End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Since you are looping through the range, and setting each cell equal to "cell", replace "ActiveCell" with cell, and do not use Select, i.e.
Code:
Sheets("Data").Select

Dim rng As Range, cell As Range
Set rng = Range("J10:J25")
For Each cell In rng

    If cell < Range("D12") Then
         With cell.Interior
             .Pattern = xlSolid
             .PatternColorIndex = xlAutomatic
             .Color = 255
             .TintAndShade = 0
             .PatternTintAndShade = 0
         End With
    End If

Next cell

End Sub
 
Last edited:
Upvote 0
Thanks a lot! It worked and I learn something along the way:)

For some reason I get a error message after the code has run.. saying error nr 13: "Incompatible types". The highlighted part is: "If cell < Range("D12") Then".

The code still works but any idea why this error occurs?
 
Last edited:
Upvote 0
What is the value of "cell" when the error occurs?
Do you have any errors in J10:J25?
 
Upvote 0
This code will tell you what cell is causing the error:
Code:
Sub MyMacro()

    Dim rng As Range, cell As Range

    Sheets("Data").Select
    Set rng = Range("J10:J25")
    
    On Error GoTo err_chk
    For Each cell In rng
        If cell < Range("D12") Then
             With cell.Interior
                .Pattern = xlSolid
                .PatternColorIndex = xlAutomatic
                .Color = 255
                .TintAndShade = 0
                .PatternTintAndShade = 0
            End With
        End If
    Next cell
    On Error GoTo 0
    
    Exit Sub
    

err_chk:
    MsgBox "Error in cell " & cell.Address(0, 0)

End Sub
Take a look at that cell and its contents.
 
Upvote 0
Thank you so much for your superb help! There was a problem in the range (a cell had a #div error. Thank you for taking the time!
 
Upvote 0
Thank you so much for your superb help! There was a problem in the range (a cell had a #div error. Thank you for taking the time!


In the following way you can evaluate if the cell has an error

Code:
    Sheets("Data").Select
    Dim cell As Range
    For Each cell In Range("J10:J25")
[COLOR=#0000ff]        If Not IsError(cell) Then[/COLOR]
            If cell < Range("D12") Then
                 With cell.Interior
                     .Pattern = xlSolid
                     .PatternColorIndex = xlAutomatic
                     .Color = 255
                     .TintAndShade = 0
                     .PatternTintAndShade = 0
                 End With
            End If
        End If
    Next cell
 
Upvote 0
You are welcome.
Glad we were able to help.
 
Upvote 0

Forum statistics

Threads
1,223,900
Messages
6,175,276
Members
452,629
Latest member
SahilPolekar

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