Changing cell colors as determined by a variable

xxsacra

New Member
Joined
Jun 3, 2010
Messages
4
The titles a bit shaky, sorry if it makes no sense :eek:

Anyway, im trying to write a code (im pretty nood at excel so bare with me here) that when i put 'Y' (as in yes) into column F it fills that whole row with red or orange. thae code im using at the moment seems to have some end if issues.
it could just be a terrible code.

tl;dr When i put 'Y' into column x i want it to fill the row with red.

the (non working) code im using right now is

Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
    If (Range("F:F").Value) = "Y" Then
            
            Rows("Target.Offset(0, 1)").Select
            With Selection.Interior
        .ColorIndex = 46
        .Pattern = xlSolid
End If
End With
End Sub
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Welcome to the Board!

Why not just use Conditional Formatting?

Select your range and then Data-->Conditional Formatting-->Formula is-->=$F1="Y"-->Format as desired.

As for your code, noting that it's not necessary, your End If/End With statements are transposed (among other things).

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If (Target.Column = 6) And Target.Value = "Y" Then
        With Rows(Target.Row).EntireRow.Interior
            .ColorIndex = 46
            .Pattern = xlSolid
        End With
    End If
End Sub

HTH,
 
Last edited:
Upvote 0
This could be done without code using conditional formatting.

Is that an option?

By the way you mentioned 2 formats but only one criteria.
 
Upvote 0
I couldn't quite figure out the formatting (I could make one cell orange but not the whole row) But that code works like a charm now (thanks smitty!!)

I really appreciate it guys.

This forum needs a +rep function :D
 
Upvote 0
No problem. Just note that it's generally wiser to go with a native solution vs. code. In this case Conditional Formatting will work just fine. :)
 
Upvote 0

Forum statistics

Threads
1,215,446
Messages
6,124,900
Members
449,194
Latest member
JayEggleton

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