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

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
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,735
Messages
6,132,423
Members
449,727
Latest member
Aby2024

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