Reference to named range not working when sub is converted to a worksheet_change sub

coldturkey

New Member
Joined
Dec 3, 2019
Messages
5
Office Version
  1. 2016
Platform
  1. Windows
The active cell has a drop-down list in it and I would like to colour the cell depending on the choice made. Unfortunately you can't pass through formatting when you use a drop-down list.
When I run the following macro as a normal sub it works fine but when I convert it into a worksheet_change sub it gives me an error on the For Each line.

The error message I received is:
Run-time error '1004'
Method' Range of object'_Worksheet failed

Can somebody please explain why and show me how to fix it?
Note: the named range carer_initials is on another worksheet but in the same workbook

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
' Sub to copy format from Carer_initials to activecell if activecell is the same value
Dim c As Range
If ActiveCell.Value <> "" Then
 For Each c In Range("Carer_initials")
 If ActiveCell.Value = c.Value Then
 ActiveCell.font.color = c.font.color
 Exit For
 End If
 Next c
End If
End Sub


My use of VBA is pretty much self-taught so I always have trouble figuring out the correct syntax of some of these.
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
You need to specify your sheet name in Range("Carer_initials") i.e. Sheets("yoursheetname").Range("Carer_initials") or it refers to the ActiveSheet, but also you should be using Target rather than Activecell if using a Worksheet_Change and do you really want it running on every cell that is Activated?
 
Upvote 0
You need to specify your sheet name in Range("Carer_initials") i.e. Sheets("yoursheetname").Range("Carer_initials") or it refers to the ActiveSheet, but also you should be using Target rather than Activecell if using a Worksheet_Change and do you really want it running on every cell that is Activated?

Thanks for your response. I tried your suggestion but all it does is convert the error into:
' Application-defined or object-defined error'

I did however find that Application.Range("Carer_initials") solves the problem neatly and the macro now works.

I have taken on board your suggestion about using Target rather than Activecell and modified the macro so it looks neater.
I don't however understand your question about it running on every cell that is activated?
Doesn't the macro run only on the Target cell? If there was a way to only have the macro run when a Target cell in columns D to AI was changed that would be fantastic.
 
Upvote 0
Please also take a minute to note the rules on cross-posting (the same here as on most other Excel forums) and follow them in future. Thanks. :)
 
Upvote 0
I have now read the rule on cross-posting.
FYI since there weren't any obvious links to the rules, I went into guidelines and there found a link to the rules. The rule itself mentioned cross-posting but didn't actually say what it was. I then clicked on the link there to get to the Excelguru help site where I found what cross-posting is.
Now I know, and it seems like a reasonable rule.
 
Upvote 0

Forum statistics

Threads
1,214,834
Messages
6,121,871
Members
449,054
Latest member
juliecooper255

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