Cell Font Color based on another Cell value [VBA]

Martin_H

Board Regular
Joined
Aug 26, 2020
Messages
190
Office Version
  1. 365
Platform
  1. Windows
Hi team,

let there be a range L4:L126.

In this range, the cells can only acquire the values W4, or W5, or W6, or W7, or JR, or the cell is just empty.

If the cell in the range - let's just say L6 - has a value W4, then color the font of the cell C6 to RGB(0, 130, 59) else,
If the cell in the range - let's just say L8 - has a value W5, then color the font of the cell C8 to RGB(0, 0, 0) else,
If the cell in the range - let's just say L25 - has a value W6, then color the font of the cell C25 to RGB(255, 0, 0) else,
If the cell in the range - let's just say L111 - has a value W7, then color the font of the cell C111 to RGB(128, 10, 246) else,
If the cell in the range - let's just say L87 - has a value JR, then color the font of the cell C87 to RGB(128, 0, 0) else,
If the cell in the range - let's just say L120 - is BLANK (empty), then color the font of the cell C120 to RGB(191, 191, 191) end.


Thank you for your help.

Much appreciated.
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Hi

You've mentioned VBA, is there a reason you can't use normal conditional formatting?

You would select range L4:L126, then in conditional formatting new rule:
1. Format Cells that Contain
2. Specific Text, equal to, W4
3. Set font colour accordingly
4. Repeat for each of your conditions.

HTH, Dave
 
Upvote 0
Try this:
VBA Code:
Sub ChangeFontColor()
Dim i As Long, Lr As Long
Lr = Range("L" & Rows.Count).End(xlUp).Row
' Convert RGB to Long = Red + Green * 256 + Blue * 65536
For i = 4 To Lr
Select Case LCase(Range("L" & i).Value)
    Case "w4"
        Range("C" & i).Font.Color = 3899904
    Case "w5"
        Range("C" & i).Font.Color = 0
    Case "w6"
        Range("C" & i).Font.Color = 255
    Case "w7"
        Range("C" & i).Font.Color = 16124544
    Case "jr"
        Range("C" & i).Font.Color = 128
    Case ""
        Range("C" & i).Font.Color = 12566463
End Select
Next i
End Sub
And if you want worksheet Change event to whenever you change text at column L Macro runs. then
Right-Click on Sheet name , Select view code and Paste this:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Long, Lr As Long
Lr = Range("L" & Rows.Count).End(xlUp).Row
' Convert RGB to Long = Red + Green * 256 + Blue * 65536
If Intersect(Target, Range("L4:L" & Lr)) Is Nothing Then Exit Sub
i = Taget.Row
Select Case LCase(Range("L" & i).Value)
    Case "w4"
        Range("C" & i).Font.Color = 3899904
    Case "w5"
        Range("C" & i).Font.Color = 0
    Case "w6"
        Range("C" & i).Font.Color = 255
    Case "w7"
        Range("C" & i).Font.Color = 16124544
    Case "jr"
        Range("C" & i).Font.Color = 128
    Case ""
        Range("C" & i).Font.Color = 12566463
End Select
End Sub
 
Upvote 0
Try this:
VBA Code:
Sub ChangeFontColor()
Dim i As Long, Lr As Long
Lr = Range("L" & Rows.Count).End(xlUp).Row
' Convert RGB to Long = Red + Green * 256 + Blue * 65536
For i = 4 To Lr
Select Case LCase(Range("L" & i).Value)
    Case "w4"
        Range("C" & i).Font.Color = 3899904
    Case "w5"
        Range("C" & i).Font.Color = 0
    Case "w6"
        Range("C" & i).Font.Color = 255
    Case "w7"
        Range("C" & i).Font.Color = 16124544
    Case "jr"
        Range("C" & i).Font.Color = 128
    Case ""
        Range("C" & i).Font.Color = 12566463
End Select
Next i
End Sub
And if you want worksheet Change event to whenever you change text at column L Macro runs. then
Right-Click on Sheet name , Select view code and Paste this:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Long, Lr As Long
Lr = Range("L" & Rows.Count).End(xlUp).Row
' Convert RGB to Long = Red + Green * 256 + Blue * 65536
If Intersect(Target, Range("L4:L" & Lr)) Is Nothing Then Exit Sub
i = Taget.Row
Select Case LCase(Range("L" & i).Value)
    Case "w4"
        Range("C" & i).Font.Color = 3899904
    Case "w5"
        Range("C" & i).Font.Color = 0
    Case "w6"
        Range("C" & i).Font.Color = 255
    Case "w7"
        Range("C" & i).Font.Color = 16124544
    Case "jr"
        Range("C" & i).Font.Color = 128
    Case ""
        Range("C" & i).Font.Color = 12566463
End Select
End Sub
Not sure why, but it only change the color if the cell is empty :unsure:
 
Upvote 0
1. W4 is Cell Address or Cell Value ??
2. Are you don't have more space at the end of your data at column L?

if you have space try changing Select Case Line code to this
VBA Code:
Select Case LCase(Trim(Range("L" & i).Value))

3. you run Normal macro or worksheet change event?
 
Upvote 0
1. W4 is Cell Address or Cell Value ??
2. Are you don't have more space at the end of your data at column L?

if you have space try changing Select Case Line code to this
VBA Code:
Select Case LCase(Trim(Range("L" & i).Value))

3. you run Normal macro or worksheet change event?
1. W4 is Cell Value.
2. No.
3. I run Worksheet change event.
 
Upvote 0
Worksheet change event only on change color of cell that value changed. For changing all cells color at first time, you should run normal macro.
 
Upvote 0
Worksheet change event only on change color of cell that value changed. For changing all cells color at first time, you should run normal macro.
Yeah, I understand.

Each time I change the value in column L, the font color in column C does not change.

This only works if the cell value in column L is empty. Then the color actually changes to .Color = 12566463 :unsure:

However, it does not respond to W4, W5, W6, W7 values ?
 
Upvote 0
Sorry my fault. Change Lr to :
VBA Code:
Lr = Range("C" & Rows.Count).End(xlUp).Row
Then Code is:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Long, Lr As Long
Lr = Range("C" & Rows.Count).End(xlUp).Row
' Convert RGB to Long = Red + Green * 256 + Blue * 65536
If Intersect(Target, Range("L4:L" & Lr)) Is Nothing Then Exit Sub
i = Target.Row
Select Case LCase(Range("L" & i).Value)
    Case "w4"
        Range("C" & i).Font.Color = 3899904
    Case "w5"
        Range("C" & i).Font.Color = 0
    Case "w6"
        Range("C" & i).Font.Color = 255
    Case "w7"
        Range("C" & i).Font.Color = 16124544
    Case "jr"
        Range("C" & i).Font.Color = 128
    Case ""
        Range("C" & i).Font.Color = 12566463
End Select
End Sub

Or if you have fixed range try:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Long
' Convert RGB to Long = Red + Green * 256 + Blue * 65536
If Intersect(Target, Range("L4:L126")) Is Nothing Then Exit Sub
i = Target.Row
Select Case LCase(Range("L" & i).Value)
    Case "w4"
        Range("C" & i).Font.Color = 3899904
    Case "w5"
        Range("C" & i).Font.Color = 0
    Case "w6"
        Range("C" & i).Font.Color = 255
    Case "w7"
        Range("C" & i).Font.Color = 16124544
    Case "jr"
        Range("C" & i).Font.Color = 128
    Case ""
        Range("C" & i).Font.Color = 12566463
End Select
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,377
Messages
6,119,182
Members
448,872
Latest member
lcaw

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