Conditional formatting Macro run time error

apnt81

New Member
Joined
Mar 3, 2011
Messages
1
Hello - my goal is to have a macro that will colour a cell a certain colour depending on a list of possible words that have been restricted using 'data validation' feature. Using a combination of own knowledge and different websites I have written a macro that works well apart from one thing.

When I copy and paste multiple cells or try to delete multiple cells I get a 'run time error' and I am not sure why.

Can someone please let me know how I need to alter the code to prevent this?

The exact text of the code is copied below:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim icolor As Integer
If Not Intersect(Target, Range("C11:C65536")) Is Nothing Then
Select Case Target
Case Is = "Project Promoted to GREY in PRISM"
icolor = 15
Case Is = "D&D Completed Work"
icolor = 36
Case Is = "To Be Worked By D&D"
icolor = 35
Case Is = "Holding for Information from Project"
icolor = 38
Case Is = "D&D Work in Progress"
icolor = 40
Case Is = "No Documentation Received"
icolor = 37

Case Is = "No Drawing Updates Required"
icolor = 6

Case Is = "Project Work Cancelled"
icolor = 14

Case Else
End Select

Target.Interior.ColorIndex = icolor
End If
End Sub

Thanks in advance for your help!

Andrew
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Welcome to MrExcel.

The problem arises because when you paste or delete multiple cells in column C, the range "Target" that is passed to the procedure is a range of multiple cells, whereas the Select Case Target statement you are using is assuming a single cell.

You can modify your code to:

Rich (BB code):
Private Sub Worksheet_Change(ByVal Target As Range)
Dim icolor As Integer
If Not Intersect(Target, Range("C11:C65536")) Is Nothing Then
For Each cell In Target
Select Case cell
Case Is = "Project Promoted to GREY in PRISM"
icolor = 15
Case Is = "D&D Completed Work"
icolor = 36
Case Is = "To Be Worked By D&D"
icolor = 35
Case Is = "Holding for Information from Project"
icolor = 38
Case Is = "D&D Work in Progress"
icolor = 40
Case Is = "No Documentation Received"
icolor = 37
Case Is = "No Drawing Updates Required"
icolor = 6
Case Is = "Project Work Cancelled"
icolor = 14
Case Else
icolor = cell.Interior.ColorIndex
End Select
cell.Interior.ColorIndex = icolor
Next
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,828
Members
452,946
Latest member
JoseDavid

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