Format every second selected row VBA

weasel21

New Member
Joined
Jul 12, 2018
Messages
6
I have the below code to apply formatting to every second selected row.

It works when i use
Rich (BB code):
Rich (BB code):
Selection.Rows(Counter).Interior.Pattern = xlGray16
but i want to apply different formatting. i want every second row coloured in blue. I have tried the below but it doesnt work. any ideas?

Rich (BB code):
Rich (BB code):
Sub ShadeEveryOtherRow()
    Dim Counter As Integer
LastRowColumnX = Cells(Rows.Count, 1).End(xlUp).Row
    Range("A15:P" & LastRowColumnX).Select
    For Counter = 1 To Selection.Rows.Count
            If Counter Mod 2 = 1 Then
            Selection.Rows(Counter).Interior
                .Pattern = Solid
                .PatternColorIndex = xlAutomatic
                .ThemeColor = xlThemeColorAccent5
                .TintAndShade = 0.599993896298105
                .PatternTintAndShade = 0
        End If
    Next
End Sub
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
You need to use a with statement
Code:
Sub ShadeEveryOtherRow()
   Dim Counter As Integer
   LastRowColumnX = Cells(Rows.Count, 1).End(xlUp).Row
   Range("A15:P" & LastRowColumnX).Select
   For Counter = 1 To Selection.Rows.Count
      If Counter Mod 2 = 1 Then
         With Selection.Rows(Counter).Interior
            .Pattern = Solid
            .PatternColorIndex = xlAutomatic
            .ThemeColor = xlThemeColorAccent5
            .TintAndShade = 0.599993896298105
            .PatternTintAndShade = 0
         End With
      End If
   Next
End Sub
 
Upvote 0
Try this.
Code:
Sub ShadeEveryOtherRow()
Dim Counter As Long
Dim LastRowCoumnX As Long

    LastRowColumnX = Cells(Rows.Count, 1).End(xlUp).Row

    Range("A15:P" & LastRowColumnX).Select

    For Counter = 15 To LastRowColumnX Step 2
        With Intersect(Range("A:P"), Rows(Counter)).Interior
                .Pattern = Solid
                .PatternColorIndex = xlAutomatic
                .ThemeColor = xlThemeColorAccent5
                .TintAndShade = 0.599993896298105
                .PatternTintAndShade = 0
        End With
    Next Counter

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,898
Messages
6,175,274
Members
452,628
Latest member
dd2

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