SOLVED

Question related to loops

Iron Contributor

Hello Everyone,

I have created code which those value greater than 10, then it highlight red in colour.

It works in 1st column, but not jump to 2nd column to highlight.

like - 

Screenshot (3766).png

 

And Here is a code - 

Screenshot (3767).png

 

 

What should i write in VBA code?

 

Please help..??

 

Here is a attach file..

 

4 Replies
best response confirmed by allyreckerman (Microsoft)
Solution

@Excel 

Try this:

Public Sub FunWithLoop()
    Dim cel As Range
    Application.ScreenUpdating = False
    For Each cel In ActiveSheet.UsedRange
        If IsNumeric(cel.Value) Then
            If cel.Value > 10 Then
               cel.Interior.Color = vbRed
            End If
        End If
    Next cel
    Application.ScreenUpdating = True
End Sub

P.S. You can also use Conditional Formatting to highlight cells with a value greater than 10.

@Hans Vogelaar 

Sir, Can we write with the help of ActiveCell.Offset which i have written the code??

If yes,

then what should i write??

@Excel 

Selecting cells in a macro is very inefficient! You should avoid it.

Thank you so much sir:smiling_face_with_smiling_eyes::smiling_face_with_smiling_eyes:
1 best response

Accepted Solutions
best response confirmed by allyreckerman (Microsoft)
Solution

@Excel 

Try this:

Public Sub FunWithLoop()
    Dim cel As Range
    Application.ScreenUpdating = False
    For Each cel In ActiveSheet.UsedRange
        If IsNumeric(cel.Value) Then
            If cel.Value > 10 Then
               cel.Interior.Color = vbRed
            End If
        End If
    Next cel
    Application.ScreenUpdating = True
End Sub

P.S. You can also use Conditional Formatting to highlight cells with a value greater than 10.

View solution in original post