VBA Code to Set Shading in Selected Rows

MadManTom

New Member
Joined
Apr 22, 2011
Messages
3
I want to shade columns A through H for any row (after row 4) whose value in column A begins with 'A_', 'T_', or 'L_'. I execute the following block in my Auto_Open macro but the result is that any row that does not contain any text in column A gets highlighted instead. Can anyone help me figure out what I'm doing wrong? I'd really appreciate it!

Dim valstart As String
Dim O As Long
Dim lastrow
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>

lastrow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row

For O = 4 To lastrow
valstart = Left(Cells(O, "A").Value, 2)
If (valstart = A_) Or (valstart = T_) Or (valstart = L_) then
Range(Cells(O, 1), Cells(O, 8)).Select

With Selection.Interior
.ColorIndex = 15
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
End With
End If
Next O
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Try

Code:
lastrow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row

For O = 4 To lastrow

    valstart = Left(Cells(O, "A").Value, 2)
    If valstart = "A_" Or valstart = "T_" Or valstart = "L_" Then

        With Range(Cells(O, 1), Cells(O, 8))

        .ColorIndex = 15
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        End With

    End If

Next O
 
Upvote 0
Peter,

Double quotes worked like a charm. I thought I had tried this before but I think I might have used single quotes instead of double. At any rate, I'm in business now. Can't thank you enough for your help and especially for responding so fast.
Go have a salty dog and relax for the rest of the weekend!

Best Regards,

Tom
 
Upvote 0
I'm very inexperienced with VBA so I'm not sure how to implement conditional formatting. Thanks, just the same, for your reply. I'll research the topic when I get a chance. Have a good weekend, and thanks!
 
Upvote 0

Forum statistics

Threads
1,224,557
Messages
6,179,507
Members
452,917
Latest member
MrsMSalt

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