Change colour of cell based on the value of another cell VBA

tanmigd

New Member
Joined
Nov 21, 2019
Messages
3
Hey,
I'm having difficulty trying to figure this out. I want to basically make cases in column (say column K) and make another column (say column O) display color.
So for example, if I write down in Column K the word "Red", I would want the color red to be displayed in column O. If i write "Blue" in column K, I want the color blue to be displayed in column O. Colors would be repeated, so I don't need infinite colors. My range is from 1:100. I've done this via conditional formatting, but I want to do this via VBA.
Any solution or advice, would be appreciated.
Thank you!
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
When do you want the color to display as entered only when you run the code?
 
Upvote 0
This is sheet code that will run automatically as entries are mad to col K.
To install sheet code:
1. Right-click the worksheet tab you want to apply it to and choose 'View Code'. This will open the VBE window.
2. Copy the code below from your browser window and paste it into the white space in the VBE window.
3. Close the VBE window and Save the workbook. If you are using Excel 2007 or a later version do a SaveAs and save it as a macro-enabled workbook (.xlsm file extension).
4. Make sure you have enabled macros whenever you open the file or the code will not run.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range
If Not Intersect(Target, Columns("K")) Is Nothing Then
    
    For Each c In Intersect(Target, Columns("K"))
        'case sensitive
        Select Case c.Value
            Case "Red": c.Offset(0, 4).Interior.Color = vbRed
            Case "Yellow": c.Offset(0, 4).Interior.Color = vbYellow
            Case "Green": c.Offset(0, 4).Interior.Color = vbGreen
            Case "Blue": c.Offset(0, 4).Interior.Color = vbBlue
            'add other colors to suit
            Case Else: c.Offset(0, 4).Interior.Color = xlNone
        End Select
    Next c
End If
End Sub
 
Upvote 0
This is sheet code that will run automatically as entries are mad to col K.
To install sheet code:
1. Right-click the worksheet tab you want to apply it to and choose 'View Code'. This will open the VBE window.
2. Copy the code below from your browser window and paste it into the white space in the VBE window.
3. Close the VBE window and Save the workbook. If you are using Excel 2007 or a later version do a SaveAs and save it as a macro-enabled workbook (.xlsm file extension).
4. Make sure you have enabled macros whenever you open the file or the code will not run.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range
If Not Intersect(Target, Columns("K")) Is Nothing Then
   
    For Each c In Intersect(Target, Columns("K"))
        'case sensitive
        Select Case c.Value
            Case "Red": c.Offset(0, 4).Interior.Color = vbRed
            Case "Yellow": c.Offset(0, 4).Interior.Color = vbYellow
            Case "Green": c.Offset(0, 4).Interior.Color = vbGreen
            Case "Blue": c.Offset(0, 4).Interior.Color = vbBlue
            'add other colors to suit
            Case Else: c.Offset(0, 4).Interior.Color = xlNone
        End Select
    Next c
End If
End Sub
It didn't work display any color when I typed in the colors in the K column. Do you have any other suggestion?
 
Upvote 0
As noted in the code it is case sensitive so when you type in red it has to match the code Red.
if this is a problem you can list all the colors in uppercase and have the code make the value upper case.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range
If Not Intersect(Target, Columns("K")) Is Nothing Then
    
    For Each c In Intersect(Target, Columns("K"))
        
        Select Case UCase(c.Value)
            Case "RED": c.Offset(0, 4).Interior.Color = vbRed
            Case "YELLOW": c.Offset(0, 4).Interior.Color = vbYellow
            Case "GREEN": c.Offset(0, 4).Interior.Color = vbGreen
            Case "BLUE": c.Offset(0, 4).Interior.Color = vbBlue
            'add other colors to suit
            Case Else: c.Offset(0, 4).Interior.Color = xlNone
        End Select
    Next c
End If
End Sub
 
Upvote 0
It didn't work display any color when I typed in the colors in the K column. Do you have any other suggestion?
I suggest you go back to the instructions for installing the code to be certain you have followed them correctly, being sure to copy the code from your browser rather than typing it in. If that doesn't work be sure you are using all caps when you type in the colors. If still not working, be sure that events are enabled following the steps below:
1. with your workbook open press Alt + F11 to open the VBE.
2. Press ctrl + g to open the Immediate Window.
3. In the Immediate Window type: "Application.EnableEvents = True" w/o the quote marks and press Enter
4. Go back and enter a color (RED, YELLOW, GREEN or BLUE) in col K and see if col O corresponding cell fills wit that color.
 
Upvote 0
I suggest you go back to the instructions for installing the code to be certain you have followed them correctly, being sure to copy the code from your browser rather than typing it in. If that doesn't work be sure you are using all caps when you type in the colors. If still not working, be sure that events are enabled following the steps below:
1. with your workbook open press Alt + F11 to open the VBE.
2. Press ctrl + g to open the Immediate Window.
3. In the Immediate Window type: "Application.EnableEvents = True" w/o the quote marks and press Enter
4. Go back and enter a color (RED, YELLOW, GREEN or BLUE) in col K and see if col O corresponding cell fills wit that color.
It worked!!!!! Thank you
 
Upvote 0
You are welcome - thanks for the reply.
 
Upvote 0

Forum statistics

Threads
1,214,789
Messages
6,121,593
Members
449,038
Latest member
Arbind kumar

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