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.
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
What are the specifics about what string tells you to use red vs. what string tells you to use yellow?

This could be done with conditional formatting. Is there any reason you need to use VBA?
 
Upvote 0
I have the conditional formatting method in use already. With this particular workbook, I share this with other users who are constantly cutting and pasting (from Excel and external sources) which renders the conditional formatting useless, because I have to continuously fix the conditional formatting. I take advantage of the VBA because it does its work behind the scenes and the user never sees the code. I do include code to delete the non-VBA conditional formatting if a user were to paste something from an Excel document that has conditional formatting before applying the VBA conditional formatting.
 
Upvote 0
Your description is pretty light on details. Put this code in the module for the worksheet with the data. Every time a value in column H is updated it will check the first three characters of the new value. If the first three are "abc" then the corresponding cell in column A will be colored red. Otherwise it will be colored yellow.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

   Dim Cell As Range
   
   For Each Cell In Target
   
      If Cell.Column = 8 Then ' H
      
         If Left(Cell, 3) = "abc" Then
            Cells(Cell.Row, "A").Interior.Color = RGB(255, 0, 0) ' red
         Else
            Cells(Cell.Row, "A").Interior.Color = RGB(255, 255, 0) ' yellow
         End If
      
      End If
      
   Next Cell
      

End Sub
 
Upvote 0
Thank you. Sorry, I forgot to mention that I have a header row. How would I start with Row 2?
 
Upvote 0
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
 
Upvote 0
Could there be multiple cells in column H changed/entered/pasted at the same time?
 
Upvote 0
Thanks, bebo021999, but I get a variable not defined error when this code attempts to execute.
 
Upvote 0
Thanks, bebo021999, but I get a variable not defined error when this code attempts to execute.
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?
 
Upvote 0
I get a variable not defined error when this code attempts to execute.
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?
 
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

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