0

I am trying to change the macro assigned to a button from a different sheet than where the button is. This is how far I've gotten:

Sub Macro1()
    ActiveSheet.Shapes.Range(Array("Button 1")).Select
    Selection.OnAction = "Macro2"
End Sub

However, the above code requres me to be on the active sheet. I have tried the following:

Sub Macro1()
    Sheet1.Shapes.Range(Array("Button 1")).Select
    Selection.OnAction = "Macro2"
End Sub

However, this will give me an "Object doesn't support this property or method" error.

Why doesn't it work when "ActiveSheet" is replaced with "Sheet1"?

And why can't I collect the two lines of code into one line?:

Sub Macro1()
    Sheet1.Shapes.Range(Array("Button 1")).OnAction = "Macro2"
End Sub

Any help would be appreciated!

1 Answer 1

1

Please, simple try:

Sheet1.Shapes("Button 1").OnAction = "Macro2"

Of course, a macro named "Macro2" should exist in a standard module. If in a sheet code module, the sheet CodeName is necessary in front of the macro name ("Sheet1.Macro2")...

Sign up to request clarification or add additional context in comments.

2 Comments

@Elio Fernandes I am afraid you do not understand what is it about, no offence... In the above way you can assign the "Macro2" Sub to a "Button 1" shape situated on Sheet1, being wherever you want to be. I mean, in a standard module on in another sheet code module, if you understand what that mean... Selecting the shape is a bad habit, anyhow, but the above code does not select anything.
Thank you so much FaneDuru. This worked! The recorded macro threw me off. Thanks for getting me back on track. :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.