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

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
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,216,076
Messages
6,128,669
Members
449,463
Latest member
Jojomen56

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