Thursday, September 15, 2011

How do you write VB code for an excel worksheet that will change a referenced cell's color?

if U22=%26quot;Yes%26quot;, then make I6 green, else white

if U23=%26quot;Yes%26quot;, then make I7 green, else white

if U24=%26quot;Yes%26quot;, then make I8 green, else white

if u25=%26quot;Yes%26quot;, then make I9 green, else white

if u26=%26quot;Yes%26quot;, then make I10 green, else whiteHow do you write VB code for an excel worksheet that will change a referenced cell's color?The following code segment could be the basis for doing this. If you are going to have a lot of entries to test, I suggest you write a sub macro to which you pass the cell address as variables instead of coding them explicitly.



If ActiveSheet.Range(%26quot;U22%26quot;).Value = %26quot;Yes%26quot; Then

With ActiveSheet.Range(%26quot;I6%26quot;)

.Value = %26quot;Green%26quot;

.Interior.ColorIndex = 4

.Interior.Pattern = xlSolid

.Interior.PatternColorIndex = xlAutomatic

End With

Else

With ActiveSheet.Range(%26quot;I6%26quot;)

.Value = %26quot;White%26quot;

.Interior.ColorIndex = 0

.Interior.Pattern = xlSolid

.Interior.PatternColorIndex = xlAutomatic

End With

End IfHow do you write VB code for an excel worksheet that will change a referenced cell's color?with thisworkbook.worksheets(%26quot;Sheet 1%26quot;)



for i = 22 to 26

if .range(%26quot;U%26quot; %26amp; i).value = %26quot;Yes%26quot; then

'this statement if you want to change the text's color

.range(%26quot;L%26quot; %26amp; i - 16).font.colorindex = GREEN_CODE

'this statement if you want to change the cell's color

.range(%26quot;L%26quot; %26amp; i - 16).interior.colorindex = GREEN_CODE

else

.range(%26quot;L%26quot; %26amp; i - 16).font.colorindex = WHITE_CODE

.range(%26quot;L%26quot; %26amp; i - 16).interior.colorindex = WHITE_CODE

end if

next i





the GREEN_CODE and WHITE_CODE are supposed to contain the code number of the colors you wanted... pls try to see the code for that



just email me if you need clarification



end withHow do you write VB code for an excel worksheet that will change a referenced cell's color?U dont need VB Code for that. You need to use Conditional Formatting using Formulas.



If you are an extensive excel user i suggest you to visit www.ExcelGoodies.Com . And try avoiding writing VB codes for everything. If you know all the features of excel you dont need to write VB. I mean u can do most of the things without using VB codes.



I am sure www.ExcelGoodies.Com Online Excel Help will be useful for you.



Good Luck