How Do I Re Name A Macro???

rockyw

Well-known Member
Joined
Dec 26, 2010
Messages
1,191
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
 

Some videos you may like

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.

rockyw

Well-known Member
Joined
Dec 26, 2010
Messages
1,191
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
 

MarcelBeug

Well-known Member
Joined
Apr 25, 2014
Messages
1,811
Sure, just adjust Set MyRange = Range("A2") to e.g. Set MyRange = Range("A2:A5")
 

dave3009

Well-known Member
Joined
Jun 23, 2006
Messages
7,002
Office Version
  1. 365
  2. 2016
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
 

rockyw

Well-known Member
Joined
Dec 26, 2010
Messages
1,191

ADVERTISEMENT

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
 

rockyw

Well-known Member
Joined
Dec 26, 2010
Messages
1,191
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.
 

dave3009

Well-known Member
Joined
Jun 23, 2006
Messages
7,002
Office Version
  1. 365
  2. 2016

ADVERTISEMENT

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:

MarcelBeug

Well-known Member
Joined
Apr 25, 2014
Messages
1,811
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"))
 

Watch MrExcel Video

Forum statistics

Threads
1,122,900
Messages
5,598,759
Members
414,257
Latest member
Vlookup_Er_No

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
Top