• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Loop through range and apply formatting based on value of cells from another range

ssuresh98

Member
Hello Gurus,
Can you please help me with the code?
Problem:
I would like to shade cells in r2 (A1:B10) with grey color based on value of cells in r1 (C1:D10, empty or non-empty).
eg: If C1 is empty, A1 should be shaded grey
If D5 is empty, B5 should be shaded grey
The code below works but it only takes value from column C and disregards values in column D.

Can you please help?

Thanks
Code:
Sub loopColor()
Dim i As Long, r1 As Range, r2 As Range

   For i = 1 To 10
     Set r1 = Range("C" & i)
     Set r2 = Range("A" & i & ":B" & i)
     If r1.Value = "" Then r2.Interior.coloridex = 16
   Next i

   End Sub
 

Attachments

  • Loopcolor.xlsm
    22.1 KB · Views: 3
Code:
Sub loopColor()
Dim i As Long, r1 As Range, r2 As Range, r3 As Range, r4 As Range
    Range("A1:B10").Interior.colorIndex = 0
        For i = 1 To 10
        Set r1 = Range("C" & i)
        Set r2 = Range("A" & i)
        Set r3 = Range("D" & i)
        Set r4 = Range("B" & i)
            If r1.Value = "" Then r2.Interior.colorIndex = 16
            If r3.Value = "" Then r4.Interior.colorIndex = 16
        Next i
End Sub
 
Another one, with same result
Code:
Sub loopColor()

    Dim c As Range
    For Each c In Range("A1:B10")
   
        If c.Offset(0, 2).Value = "" Then c.Interior.ColorIndex = 16
   
    Next c
   
End Sub
 
Back
Top