• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Apply conditional formating in VBA

arishy

Member
I need to BG a cell based on it's value
IF A,B or C it is green
IF D,E,F it is Blue etc

So, once I past a value in a cell I need to call a function to evaluate it then give it the proper BG color.
 
Right-click on the sheet tab, view code, paste this in. Modify as needed.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim MyRange As Range
'What range do we care about?
Set MyRange = Range("A2:B10")
 
'Not a cell we care about?
If Intersect(Target, MyRange) Is Nothing Then Exit Sub
'Don't do anything if more than 1 cell changed
If Target.Count > 1 Then Exit Sub
 
'In case we run into trouble, make sure events get turned back on
On Error GoTo MyReset
Application.EnableEvents = False
Application.ScreenUpdating = False
Dim myValue As String
myValue = UCase(Target.Value)
 
 
 
'We can give multiple possibilities in each case to
'indicate an "or" type logic
 
Select Case myValue
    Case "A", "B", "C"
        Target.Interior.Color = vbGreen
    Case "D", "E", "F"
        Target.Interior.Color = vbBlue
    Case Else
        Target.Interior.Color = xlNone
End Select
 
MyReset:
Application.EnableEvents = True
Application.ScreenUpdating = True
   
End Sub
 
Back
Top