Use VBA LEFT Function to determine background color in another column

Jaye Cavallo

New Member
Joined
Mar 10, 2022
Messages
32
Office Version
  1. 2016
Platform
  1. Windows
Would someone be able to assist:

In an Excel sheet, I would like to color code a range of cells in column A, red or yellow, based on the string of text in column H. What I am seeking is to use the LEFT function in column H in VBA to determine is column A is red or yellow, based upon the first 3 characters in column H.
 
Below code triggers change in column H, from row 2.
It uses Lcase for non- case sensitive , like "abc", "Abc" or "ABC". Remove it if it was case-sensitive.
VBA Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Cell As Range, r As String, y As String
r = "abc" ' change "abc" to actual 3 characters of red
y = "def" ' change "def" to actual 3 characters of yellow
If Intersect(Target, Columns("H")) Is Nothing Or Target.Row = 1 Or Target.Count > 1 Then Exit Sub
With Target.Offset(, -7).Interior
    .Color = xlNone
    Select Case LCase(Left(Target, 3))
        Case r
            .Color = RGB(255, 0, 0) ' red
        Case y
            .Color = RGB(255, 255, 0) ' yellow
    End Select
End With
End Sub

Are there any text string in column H with less than 3 characters?
Could you attach a screenshot/XL2BB to see what data in column H look like?
While removing all other code in the worksheet_Change event, you code does not produce the variable not defined error. However, nothing seems to happens when the change event runs.

These are the values in Column H:

Gas-New Construction
Gas-Paving
Gas-Direct Mail
Electric- 69kV (Pocket)
Electric-Underground
Electric-Reliability (Pocket)
Electric-RPV
Electric-MTB
Electric-Direct Mail
 
Upvote 0

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Have you changed r ="Gas" and y = "Ele" in code?
What the current updated code look like?
 
Upvote 0
That does not happen for me. Did you modify the code in any way? Which variable is highlighted when you get that message?

BTW, what is the answer to my question in post #7?
Sorry, yes it is possible that there could be multiple cells in column H changed/entered/pasted at the same time.
 
Upvote 0
yes it is possible that there could be multiple cells in column H changed/entered/pasted at the same time.
In that case give this a try as your worksheet change event code.
I have taken a guess at red of "gas" and yellow for "ele". Adjust accordingly.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim Changed As Range, c As Range
  
  Set Changed = Intersect(Target, Columns("H"))
  If Not Changed Is Nothing Then
    For Each c In Changed
      With Range("A" & c.Row)
        Select Case UCase(Left(c.Value, 3))
          Case "GAS": .Interior.Color = vbRed
          Case "ELE": .Interior.Color = vbYellow
          Case Else: .Interior.Color = xlNone
        End Select
      End With
    Next c
  End If
End Sub
 
Upvote 0
Solution
In that case give this a try as your worksheet change event code.
I have taken a guess at red of "gas" and yellow for "ele". Adjust accordingly.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim Changed As Range, c As Range
 
  Set Changed = Intersect(Target, Columns("H"))
  If Not Changed Is Nothing Then
    For Each c In Changed
      With Range("A" & c.Row)
        Select Case UCase(Left(c.Value, 3))
          Case "GAS": .Interior.Color = vbRed
          Case "ELE": .Interior.Color = vbYellow
          Case Else: .Interior.Color = xlNone
        End Select
      End With
    Next c
  End If
End Sub
I copied your code into a new, blank workbook. Created some content in column H (Gas, Electric, Gas, Electric, etc..). Nothing happens to column A.
 
Upvote 0
I copied your code into a new, blank workbook.
Where, exactly? It needs to go into the module of the relevant worksheet. For me, that is in the Sheet1 module as shown.

1663848207706.png
 
Upvote 0
Then can you upload that sample file to DropBox or OneDrive or Google drive etc and provide a shared link here so we can take a look to investigate?
 
Upvote 0
Thanks for the sample file. The code that I provided is working fine. However, you have combined it with other code that you didn't tell us about and that code is removing the colour that my code applied. ;)

Try making this change in your code.
Rich (BB code):
WS.Range("A2:z3569").Cells.Interior.ColorIndex = xlNone
WS.Range("B2:z3569").Cells.Interior.ColorIndex = xlNone

For any values already in column H you will need to re-enter them. For example, you could Copy -> Paste values in column H all at once
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,744
Members
448,989
Latest member
mariah3

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