Looping to Format Cells

dcoledc

Active Member
Joined
May 6, 2010
Messages
403
I have the following code in the worksheet change mod and I want to clean it up:

Code:
If Range("B13") = 1 Then
    Range("U6:Y6").Interior.Color = vbYellow
Else
    Range("U6:Y6").Interior.Color = vbWhite
End If
If Range("C13") = 1 Then
    Range("U7:Y7").Interior.Color = vbYellow
Else
    Range("U7:Y7").Interior.Color = vbWhite
End If
If Range("D13") = 1 Then
    Range("U8:Y8").Interior.Color = vbYellow
Else
    Range("U8:Y8").Interior.Color = vbWhite
End If
If Range("E13") = 1 Then
    Range("U9:Y9").Interior.Color = vbYellow
Else
    Range("U9:Y9").Interior.Color = vbWhite
End If
If Range("F13") = 1 Then
    Range("U10:Y10").Interior.Color = vbYellow
Else
    Range("U10:Y10").Interior.Color = vbWhite
End If
If Range("G13") = 1 Then
    Range("U11:Y11").Interior.Color = vbYellow
Else
    Range("U11:Y11").Interior.Color = vbWhite
End If
If Range("H13") = 1 Then
    Range("U12:Y12").Interior.Color = vbYellow
Else
    Range("U12:Y12").Interior.Color = vbWhite
End If

That is actually only about 1/3 of the code. I repeat that over and over.

I did not use conditional formatting b/c I copy and paste these cells over and over and when I do that the formatting goes with it and bogs excel down. I couldn't do paste special b/c some of the cells are merged. Thus I resorted to VBA.

There has got to be a better way to do what I am doing, perhaps a loop.
 
Thanks again, I will give that a try. Currently, though, I am trying to eliminate the merged cells, as I like your orignal suggestion.
 
Upvote 0

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Success! Accept for one sheet, I was able to eliminate the merged cells. I took the code you gave and was able to make it work. Whether, my solution is the best or not, I don't know, but it works. I got the idea from your last post.:)

Code:
    Dim rng As Range, cell As Range
    If Not Intersect(Range("B15:H15, B20:M20, B24:H24"), Target) Is Nothing Then
    
        ActiveSheet.Unprotect Password:="17Lj041kM"
        
        For Each cell In Intersect(Range("B15:H15, B20:M20, B24:H24"), Target)
            
            Select Case cell.Row
                Case 15
                If cell.Column <= 2 Then
                    Set rng = Range("T6:U6").Offset(cell.Column - 2)
                Else
                    Set rng = Range("T6:U6").Offset(cell.Column - 1)
                End If
                Case 20
                    Set rng = Range("T16:U16").Offset(cell.Column - 2)
                Case 24
                If cell.Column <= 2 Then
                    Set rng = Range("T30:U30").Offset(cell.Column - 3)
                Else
                    If cell.Column = 7 Then
                        Set rng = Range("T30:U30").Offset(cell.Column - 1)
                    Else
                        If cell.Column = 8 Then
                            Set rng = Range("T30:U30").Offset(cell.Column - 0)
                        Else
                            Set rng = Range("T30:U30").Offset(cell.Column - 2)
                        End If
                    End If
                End If
            End Select
            If cell.Value = 1 Then
                rng.Interior.Color = vbYellow
            Else
                rng.Interior.Color = vbWhite
            End If
            
        Next cell

Thanks again, chalk it up to another problem solved.:)
 
Upvote 0

Forum statistics

Threads
1,224,616
Messages
6,179,908
Members
452,949
Latest member
beartooth91

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