0
votes

Can you tell me how change Excel cell colors using VBA ? When a value is OK -> cell color is green. When a value is NOTOK -> cell color is red.

2
Use Conditional Formatting, no VBA required.Scott Craner

2 Answers

1
votes

Use the .Interior.Color property of the range.

Dim ws As Excel.Worksheet
Set ws = Application.ActiveSheet

If ws.Range("A1").Value = "OK" Then
    ws.Range("A1").Interior.Color = 5287936
Elseif ws.Range("A1").Value = "NOTOK" Then 
    ws.Range("A1").Interior.Color = 255
End if
0
votes

You can use Conditional Formatting when you edit the contents of the worksheet. If you want to use VBA, and there are good reasons to do so, simply turn on the macro recorder, edit the contents of the worksheet, then look at the macro for the VBA code. Excel will write the code for you: it's a great way to learn VBA and get what you need in an efficient manner.