How Do I Re Name A Macro???

rockyw

Well-known Member
Joined
Dec 26, 2010
Messages
1,196
Office Version
  1. 2010
Platform
  1. Windows
I want to use this code several times on the same sheet with different cells. I get an error saying Ambiguous name detected. I need to rename each differently? How would I change the name without stopping the code? Thanks
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
Set MyRange = Range("A2")  'edit to suit range of cells
If Not Intersect(Target, MyRange) Is Nothing Then
If Target < 0 Then
Target.Offset(0, 5) = Date
Else
Target.Offset(0, 5).ClearContents
End If
End If
End Sub
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Reading tells me maybe I cannot use 2 of these on one sheet? That's bad. Can I add to it and look at multiple cells? Also look at A3, A4, A5 ect wit the same code? Thanks
 
Upvote 0
Hi Rocky

You can only use an event once so you would need to adapt the code to run on several sets of cells, you can set the variable many times including one after the other or in a loop, that's why it's a variable ;)

What do you need the current code to do, perhaps we can point you in the right direction :D

Regards

Dave
 
Upvote 0
Yes that works great. Now can I add a different column? (A1:A5) and (H1:H5) I'm not finding how to hook these together? Thanks
 
Upvote 0
To expand, I need to run the code on B4:B27 and J4:J27 and R4:R27. These are all on the same sheet? Can this be done. Thanks for the help.
 
Upvote 0
Like so :D

Code:
Set MyRange = Range("A1:A5, H1:H5")  'edit to suit range of cells

Just add more ranges or cells to suit
 
Last edited:
Upvote 0
Assuming it's all the same code to be executed, you can use Union, like
Code:
Set MyRange = Union(Range("B4:B27"), Range("J4:J27"), Range("R4:R27"))
 
Upvote 0
This worked great for me, thanks for the help.
 
Upvote 0

Forum statistics

Threads
1,214,421
Messages
6,119,392
Members
448,891
Latest member
tpierce

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