VBA Conditional Formatting

ricci

New Member
Joined
Sep 17, 2010
Messages
11
I'm working on a sheet that I need to have conditional formatting with more than 3 criteria in a certain range. I've worked out that I need to do this using VBA, I've put the code below and works fine.

I want to add in a 4th case, where if the contents of a cell in the range is deleted that the formatting is removed - I've tried Case ClearContents Case "" and Case " " but to no avail...any suggestion would be gratefully recieved! Thanks!



Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Dim WatchRange As Range
Dim CellVal As Integer
If Target.Cells.Count > 1 Then Exit Sub
If Target = "" Or Not IsNumeric(Target) Then Exit Sub
CellVal = Target

Set WatchRange = Range("A1:A65536")

If Not Intersect(Target, WatchRange) Is Nothing Then
Select Case CellVal
Case 0
Target.Interior.ColorIndex = 35
Case 1
Target.Interior.ColorIndex = 35
Case 2
Target.Interior.ColorIndex = 44
Case 3
Target.Interior.ColorIndex = 3

End Select

End If

End Sub
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Something like this,,,

Code:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Dim WatchRange As Range
Dim CellVal As Integer
If Target.Cells.Count > 1 Then Exit Sub
If Target = "" Then
Target.Interior.ColorIndex = xlNone
Exit Sub
End If
If Not IsNumeric(Target) Then Exit Sub

CellVal = Target

Set WatchRange = Range("A1:A65536")

If Not Intersect(Target, WatchRange) Is Nothing Then
Select Case CellVal
Case 0
Target.Interior.ColorIndex = 35
Case 1
Target.Interior.ColorIndex = 35
Case 2
Target.Interior.ColorIndex = 44
Case 3
Target.Interior.ColorIndex = 3
End Select
End If
End Sub
 
Upvote 0
Correction IN RED

Rich (BB code):
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Dim WatchRange As Range
Dim CellVal As Integer
If Target.Cells.Count > 1 Then Exit Sub
If Target = "" And Target.Column = 1 Then
Target.Interior.ColorIndex = xlNone
Exit Sub
End If
......
 
Upvote 0

Forum statistics

Threads
1,224,584
Messages
6,179,687
Members
452,938
Latest member
babeneker

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