VBA to Change Cell Color Based on Value in Excel (3 Easy Methods)

Suppose you have the following dataset:


Method 1 – Apply VBA to Change Cell Color in Excel Based on Filled Value

Steps:

  • Go to the Developer tab and select Visual Basic. This will open the Visual Basic window.

Apply VBA to Change Cell Color in Excel Based on Filled Value

  • Select Insert and then select Module in the Visual Basic window. The Module window will appear.

Apply VBA to Change Cell Color in Excel Based on Filled Value

  • Type the following code in the Module window:
Sub Change_Cell_Color()
Dim xCell As Range
Dim CommentValue As String
Dim CommentRange As Range
Set CommentRange = Range("D5:D9")
For Each xCell In CommentRange
CommentValue = xCell.Value
Select Case CommentValue
    Case "Good"
    xCell.Interior.Color = RGB(0, 255, 0)
    Case "Average"
    xCell.Interior.Color = RGB(255, 255, 0)
    Case "Poor"
    xCell.Interior.Color = RGB(255, 0, 0)
End Select
Next xCell
End Sub

Apply VBA to Change Cell Color in Excel Based on Filled Value

  • Press Ctrl + S to save the code and close the Visual Basic window.
  • Select Macros from the Developer ribbon and the Macro window will open.

Apply VBA to Change Cell Color in Excel Based on Filled Value

  • Select the applicable code and click Run.

Apply VBA to Change Cell Color in Excel Based on Filled Value

Cells should now be properly colored.

Apply VBA to Change Cell Color in Excel Based on Filled Value

Read More: Excel Formula Based on Cell Color


Method 2 – Use the Excel Command Button to Change Cell Color Based on the Value of Another Cell

Steps:

  • Go to the Developer tab.
  • Select Insert and a drop-down menu will open.
  • Select Command Button from the ActiveX Controls.

Use Excel Command Button to Change Cell Color Based on Value of Another Cell

  • Create a command button similar to the example below.

Use Excel Command Button to Change Cell Color Based on Value of Another Cell

  • Double-click on the command button and the Code window will appear inside the Visual Basic window.

Use Excel Command Button to Change Cell Color Based on Value of Another Cell

  • Type the following code in the Code window:
Private Sub CommandButton1_Click()
Dim x As Long, xCell1 As Range, xCell2 As Range
For x = 5 To 9
Set xCell1 = Range("C" & x)
Set xCell2 = Range("D" & x)
If xCell1.Value >= 2500 And xCell1.Value < 2650 Then _
xCell2.Interior.Color = vbRed
If xCell1.Value >= 2650 And xCell1.Value < 3000 Then _
xCell2.Interior.Color = vbYellow
If xCell1.Value >= 3000 And xCell1.Value < 3300 Then _
xCell2.Interior.Color = vbGreen
Next x
End Sub

Use Excel Command Button to Change Cell Color Based on Value of Another Cell

  • Press Ctrl + S to save the code and close the Visual Basic window.
  • Deselect Design Mode in the Developer ribbon.

Use Excel Command Button to Change Cell Color Based on Value of Another Cell

  • Click on the command button to change cell colors.

Use Excel Command Button to Change Cell Color Based on Value of Another Cell


Method 3 – Dynamically Change Cell Color Based on Value with Excel VBA

Steps:

  • Go to the Developer tab and select Visual Basic. It will open the Visual Basic window.

Dynamically Change Cell Color Based on Value with Excel VBA

  • Select the sheet where you want to apply the VBA code and right-click on it. A drop-down menu will appear.
  • Select View Code from the drop-down menu.

  • Type the following code in the Code window:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim xRange As Range
Dim R As Long
If Not Intersect(Range("B:C"), Target) Is Nothing Then
    Application.EnableEvents = False
    Application.ScreenUpdating = False
For Each xRange In Intersect(Range("B:C"), Target)
    R = xRange.Row
    If Range("B" & R) = Range("C" & R) Then
       Range("C" & R).Interior.ColorIndex = xlColorIndexNone
    Else
       Range("C" & R).Interior.ColorIndex = 3
    End If
Next xRange
Application.ScreenUpdating = True
Application.EnableEvents = True
End If
End Sub

Dynamically Change Cell Color Based on Value with Excel VBA

  • Press Ctrl + S to save the code and close the Visual Basic window.
  • Type a value in the appropriate cell (B2) and the other cell (C2) will automatically change to red.

  • Enter a value in the second cell (C2) and press Enter. Since the names are different, the cell remains red.

If both cells have the same value, the color will not change.

 

Read More: How to Change Cell Color Based on a Value in Excel


Things to Remember

After running the VBA codes, if you change any values, the result will not be automatically updated. You need to run the code again to see the updated result.


Download Practice Book

Download the practice book here.


Related Articles


<< Go Back to Color Cell in Excel | Excel Cell Format | Learn Excel

Get FREE Advanced Excel Exercises with Solutions!
Mursalin Ibne Salehin
Mursalin Ibne Salehin

Mursalin Ibne Salehin holds a BSc in Electrical and Electronics Engineering from Bangladesh University of Engineering and Technology. Over the past 2 years, he has actively contributed to the ExcelDemy project, where he authored over 150 articles. He has also led a team with content development works. Currently, he is working as a Reviewer in the ExcelDemy Project. He likes using and learning about Microsoft Office, especially Excel. He is interested in data analysis with Excel, machine learning,... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo