VBA Colour rows in a range containing data with 2 alternating RGB Colours

Event2020

Board Regular
Joined
Jan 6, 2011
Messages
122
Office Version
  1. 2019
Platform
  1. Windows
Excel 2019
Windows 10

I wish to colour all rows, from Column A - Column G that contains data
using 2 alternaing RGB colours on a specific called Results.

I want to do this using VBA as I am using VBA to pull data from various other
worksheets into the Results sheet.

so...
Row 1 = RGB(179, 230, 255)
Row 2 = RGB(255, 255, 197)
Row 3 =RGB(179, 230, 255)
Row 4 = RGB(255, 255, 197)

and so on down the columns.

I have a existing VBA code (macro) that colours a different sheet based on matching rows but I am struggling
to adapt it to the criteria above.
VBA Code:
Sub ColourAlternateRows()
    With Application
        .ScreenUpdating = False
    End With

    Dim Cl As Range
    Dim lngRow As Long
     
    With CreateObject("scripting.dictionary")
        For Each Cl In Range("E2", Range("E" & Rows.Count).End(xlUp))
            If Not .Exists(Cl.Value) Then .Add Cl.Value, (.Count Mod 2)
            lngRow = Cl.Row
            If .Count Mod 2 = 0 Then
                Range(Cells(lngRow, "A"), Cells(lngRow, "G")).Interior.Color = RGB(179, 230, 255)
            Else
                Range(Cells(lngRow, "A"), Cells(lngRow, "G")).Interior.Color = RGB(255, 255, 197)
            End If
        Next Cl
    End With
    
    With Application
        .ScreenUpdating = True
    End With
 
End Sub

While searching this forum (and google) before posting I found this thread HERE
The user was asking how to colour every other row only and again I have struggled to adapt it to my needs.
VBA Code:
Sub ColorAlternate()
Dim LR As Long, i As Long
'Stop the screen from flickering
Application.ScreenUpdating = False
'Find the last filled row in column A
LR = Range("A" & Rows.Count).End(xlUp).Row
'Loop through the filled rows in steps of 2
For i = 2 To LR Step 2
'Colour alternate rows
    Rows(i).EntireRow.Interior.ColorIndex = 6
Next i
'Turn screen updating on again
Application.ScreenUpdating = True
End Sub

Could someone kindly help me out?
 
For future readers, solution is to change starting point of i from
For i = 1 To lngRow to For i = 2 To lngRow
because i represents the row number.
 
Upvote 0
Solution

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.

Forum statistics

Threads
1,223,231
Messages
6,170,885
Members
452,364
Latest member
springate

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