Applying VBA Formatting to a selected range of cells with Validation

L

Legacy 102432

Guest
Hi,

I am not a genius on VBA and I have only recently started teaching myself a thing or two about it. Basically what I needed to do was apply more than 3 conditional formats to a range of cells.

I have drop down cells that represent the "Status" for Projects that I am working on.. I have 5 Status options that I want to be able to choose from, and when these are selected, I want the background of the cell to change colour as below:

When user selects:
Ongoing = Yellow
Not Started = Grey
On Schedule = Orange
Behind = Red
Completed = Green

I copied this VBA code from the internet that allows me to do this:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim icolor As Integer
If Not Intersect(Target, Range("E7:E14")) Is Nothing Then
Select Case Target
Case ""
icolor = 2
Case "Ongoing"
icolor = 6
Case "Not Started"
icolor = 15
Case "On Schedule"
icolor = 45
Case "Behind"
icolor = 3
Case "Completed"
icolor = 4
Case Else
'Whatever
End Select

Target.Interior.ColorIndex = icolor
End If

End Sub



My only problem is that if someone highlights more than one cell in that range and hits the delete key, I get an error message

"Run-time error '13':

Type mismatch"

Then the option to either End or Debug.

It will only allow the user to delete the contents of the cell individually. Is there a way to stop this error message from appearing?

Any help would be appreciated!
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Hi and welcome to the board!!!

Make this the 1st line of your code
Code:
If Target.Count > 1 Then Exit Sub

Also, you can write your cases like this
Code:
Case "Completed":iColor = 4
Saves space and easier to read

lenze
 
Last edited:
Upvote 0
Hi and welcome to the board!!!

Make this the 1st line of your code
Code:
If Target.Count > 1 Then Exit Sub


Thanks that stops the error message, but the background colour for the cells remain. How do I keep the background white after the user has deleted the contents?
 
Upvote 0
Mabe this will help?
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim icolor As Variant
If Intersect(Target, Range("E7:E14")) Is Nothing Then Exit Sub
Select Case Target.Count
Case 1
    Select Case Target
        Case "": icolor = xlNone
        Case "Ongoing": icolor = 6
        Case "Not Started": icolor = 15
        Case "On Schedule": icolor = 45
        Case "Behind": icolor = 3
        Case "Completed": icolor = 4
        Case Else 'Whatever
    End Select
    Target.Interior.ColorIndex = icolor
Case Else: Target.Interior.ColorIndex = xlNone
End Select
End Sub

lenze
 
Upvote 0

Forum statistics

Threads
1,215,430
Messages
6,124,850
Members
449,194
Latest member
HellScout

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