Home > OS >  the macro may not be available in this workbook
the macro may not be available in this workbook

Time:03-25

I've spent a lot of time reading through the various threads on this topic, and can't figure out what I'm doing wrong. I pasted this code (after inserting a blank rectangle) into a new workbook, and it worked. But in my current workbook, the tester sub runs fine, but when I call doStuff from the button, it throws an error (outlined in the title of this question).

Ive checked security, and again, new workbook, same code, works perfectly. It can't be the macro doesn't exist, because the scope is very small, and I can see both subs.

Sub tester()
    
    ActiveSheet.Shapes.Range(Array("Rectangle 1")).Select
    Selection.OnAction = "doStuff"

End Sub

Sub doStuff()

    MsgBox "hello"

End Sub

CodePudding user response:

If you are trying to bind a macro to a shape, here is the code you are looking for:

Sub assign_macro_to_shape()
    ActiveSheet.Shapes("Rectangle 1").OnAction = "doStuff"
End Sub

CodePudding user response:

You need to assign the "doDtuff" macro to the rectangle. Right-click the rectangle and choose "assign macro" as seen here. Then pick the "doStuff" macro.

enter image description here

  • Related