Colour alternate rows with unknown range

Apple08

Active Member
Joined
Nov 1, 2014
Messages
450
Hi All

I want to change the color of the alternate rows starting from A3, but with unknown number of columns and rows. I have found the code below but I don't know how to apply to an unknown range, please could I have some advice?

Code:
Dim r As Range
    Set r = Range("A3:A20")
    Dim tmp As Range, i As Integer

    For Each tmp In r.Cells
        i = i + 1
        If i Mod 2 = 0 Then tmp.Interior.Color = RGB(127, 187, 199)
    Next tmp
 
Last edited:

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
So you do not know what rows or what columns and did not say what color you want these rows.
So how would we know?
 
Upvote 0
.
Here is code that changes every 3rd row a selected color. You can change the Mod from 3 to 2 for everyother row.

You will manually highlighted the range desired, then make the selection from a Combobox. If you don't want a Combobox you can edit the macro for any type control preferred.
If you want to use only one color, forego most all of the macro and use just one section.

Code:
Private Sub ComboBox2_Change()
Dim Counter As Integer


Dim index As Integer
index = ComboBox2.ListIndex


Select Case index
    
    Case Is = 0
       'For every row in the current selection...
       For Counter = 1 To Selection.Rows.Count
           'If the row is an odd number (within the selection)...
           If Counter Mod 3 = 1 Then
               'Set the pattern to Lt Grey.
               Selection.Rows(Counter).Interior.Color = RGB(192, 192, 192)
           End If
       Next
    Case Is = 1
        'For every row in the current selection...
       For Counter = 1 To Selection.Rows.Count
           'If the row is an odd number (within the selection)...
           If Counter Mod 3 = 1 Then
               'Set the pattern to Lt Green.
               Selection.Rows(Counter).Interior.Color = RGB(204, 255, 204)
           End If
       Next
    Case Is = 2
        'For every row in the current selection...
       For Counter = 1 To Selection.Rows.Count
           'If the row is an odd number (within the selection)...
           If Counter Mod 3 = 1 Then
               'Set the pattern to Lt Yellow.
               Selection.Rows(Counter).Interior.Color = RGB(255, 255, 153)
           End If
       Next
    Case Is = 3
        'For every row in the current selection...
       For Counter = 1 To Selection.Rows.Count
           'If the row is an odd number (within the selection)...
           If Counter Mod 3 = 1 Then
               'Set the pattern to Lt Blue.
               Selection.Rows(Counter).Interior.Color = RGB(204, 255, 255)
           End If
       Next
    Case Is = 4
        'For every row in the current selection...
       For Counter = 1 To Selection.Rows.Count
           'If the row is an odd number (within the selection)...
           If Counter Mod 3 = 1 Then
               'Set the pattern to Lt Tan.
               Selection.Rows(Counter).Interior.Color = RGB(255, 204, 153)
           End If
       Next
    Case Is = 5
        'For every row in the current selection...
       For Counter = 1 To Selection.Rows.Count
           'If the row is an odd number (within the selection)...
           If Counter Mod 3 = 1 Then
               'Set the pattern to Lt Red.
               Selection.Rows(Counter).Interior.Color = RGB(255, 220, 204)
           End If
       Next
     Case Is = 6
        'For every row in the current selection...
       For Counter = 1 To Selection.Rows.Count
           'If the row is an odd number (within the selection)...
           If Counter Mod 3 = 1 Then
               'Set the pattern to White.
               Selection.Rows(Counter).Interior.Color = RGB(255, 255, 255)
           End If
       Next
    
End Select


End Sub
 
Upvote 0
Apple08

Give this a try

Dim celda As Range
Dim i As Integer
For Each celda In Selection
i = i + 1
If i Mod 2 = 0 Then celda.Interior.Color = RGB(127, 187, 199)
Next celda
 
Upvote 0
Sorry guys, I haven't explained clearly. I want to start from row 3 then change te the alternate row every two rows in light yellow colour. However the number of rows and columns are unknown for the report.

ColdGeorge, your code only change the alternate column but not row. Also it only color up to row 2.
 
Upvote 0
Hello Apple08 :biggrin:
try this
Code:
Sub colooor()
Dim r As Range
Dim i As Integer
    For i = 3 To 30000 Step 2
    If Cells(i, 1) <> "" Then
    Cells(i, 1).EntireRow.Interior.Color = RGB(255, 255, 153)
    End If
    Next i
End Sub
(y)
 
Last edited:
Upvote 0
Thanks r1998, it works but it doesn't stop at the last column Is it possible to make it stop there?
 
Upvote 0
Indeed, the last column is unknown. However if it is too complicated to stop at an unknown column, please can I make it stop at column AH?
 
Upvote 0
Try this:
Code:
Sub colooor()
'Modified  9/26/2018  5:16:16 AM  EDT
'r1998 Original code modified
Dim r As Range
Dim i As Integer
Dim LastColumn As Long
    For i = 3 To 30000 Step 2
        If Cells(i, 1) <> "" Then
            Cells(i, 1).Resize(, Cells(i, Columns.Count).End(xlToLeft).Column).Interior.Color = RGB(255, 255, 153)
        End If
    Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,862
Members
449,052
Latest member
Fuddy_Duddy

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