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
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Try

Code:
Sub yella()
Dim LR As Long, I As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
For I = 1 To LR
    With Range("B" & I)
        .Value = IIf(.Offset(, -1).Interior.ColorIndex = 6, "y", "n")
    End With
Next I
End Sub
 
Upvote 0
My code will only work with cells that are coloured manually, not with Conditional Formatting.

If the cells are manually coloured, click in a yellow cell and run this

Code:
Sub ColCode()
MsgBox ActiveCell.Interior.ColorIndex
End Sub

Replace the 6 in the code I posted earlier with the returned value.
 
Upvote 0
Use the same formula you used in Conditional formatting. Whatever functionality you used in Conditional formatting can be passed through VBA
 
Upvote 0
Ryan,

I am not following what you are saying, I have actually three columns:

Column A Column B Column C
11 12 n
15 14 y

The conditional formatting I have in Column B is if =$A2>$B2 then use background color yellow.

What I want is just "y" for yes they have a background color of yellow or "n" if they do not have a background color of yellow.
 
Upvote 0
It would have helped if you had said exactly what you wanted, without dumming down the question, in the first place.

Code:
Sub yella()
Dim LR As Long, I As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
For I = 1 To LR
    With Range("C" & I)
        .Value = IIf(.Offset(, -2).Value > .Offset(, -1).Value, "y", "n")
    End With
Next I
End Sub
 
Upvote 0
Look at Vog's code and I'll try to walk you through it

Code:
Sub yella()
Dim LR As Long, I As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
For I = 1 To LR
   If Cells(i,1).value = Cells(i,2).value then
        Cells(i,3) = "y"
   Else
        Cells(i,3).value = "n"
   Endif 
Next I
End Sub

Vog's initial formula was checking the selected cell for a background color, the above modified code (the If Statement) should test Col A vs Col B and put the correct value in C. test it out
 
Upvote 0
VoG,

I apologize, I appreciate your time. I forgot to mention an important factor to the question that's my fault.
 
Upvote 0

Forum statistics

Threads
1,215,059
Messages
6,122,917
Members
449,093
Latest member
dbomb1414

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