VBA code for if cell Background color is yellow then give me "y" if not "n"

boldcode

Active Member
Joined
Mar 12, 2010
Messages
347
Hi all,

Here is my situation, I have two columns A and B:

Column A Column B
12 y
14 n

If cells in Column A have a background color of yellow, then in the adjacent cells in Column B give a "y" for yes they have a background color of yellow or "n" if they do not have a background color of yellow.

Thanks in advance!

BC
 
VoG and Ryan,

It works just fine and I appreciate your time except for my column header keeps changing to n
 
Upvote 0

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Just make this small change

Rich (BB code):
Sub yella()
Dim LR As Long, I As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
For I = 2 To LR
    With Range("C" & I)
        .Value = IIf(.Offset(, -2).Value > .Offset(, -1).Value, "y", "n")
    End With
Next I
End Sub
 
Upvote 0
figured it out guys, I just changed For I = 1 To LR to For I = 2 To LR

Sub yella()
Dim LR As Long, I As Long
LR = Range("B" & Rows.Count).End(xlUp).Row
For I = 2 To LR
With Range("D" & I)
.Value = IIf(.Offset(, -2).Value > .Offset(, -1).Value, "y", "n")
End With
Next I
End Sub
 
Upvote 0
VoG and Ryan,

Thank you!!! It works just like you guys said, I apologize for not clarifying my question accurately.

Again, Thank you,

BC
 
Upvote 0
What changes the values in columns A and B - you or do they contain formulas?
 
Upvote 0
VoG,

I change the values in A and in B their is a formula. So, the code you gave me works great but I the next thing I am looking for is: when I change the value in cells A I the macro you gave to run automatically without me having to run manually.


Sub yella()
Dim LR As Long, I As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
For I = 2 To LR
With Range("C" & I)
.Value = IIf(.Offset(, -2).Value > .Offset(, -1).Value, "y", "n")
End With
Next I
End Sub</pre>
 
Upvote 0
Try this: right click the sheet tab and select View Code. Paste in

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 1 Then
    Application.EnableEvents = False
    With Target.Offset(, 2)
        .Value = IIf(.Offset(, -2).Value > .Offset(, -1).Value, "y", "n")
    End With
    Application.EnableEvents = True
End If
End Sub
then press ALT + Q to return to your sheet.
 
Upvote 0

Forum statistics

Threads
1,215,577
Messages
6,125,640
Members
449,242
Latest member
Mari_mariou

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