Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

VBA Pass argument with .Onaction with parameter

I’ve read other articles on this topic and their proposed solution is not working for me:

Sub doStuff()

    Dim x As String
    x = "hello"
    Sheets("sheet2").Shapes(1).OnAction = "'testIt " & x & "'"

End Sub

Sub testIt(something As String)

End Sub

When I click on Shapes(1) in sheet2, I get the following error response:
Cannot run macro "testIt hello". The macro may not be available in this workbook or all macros may be disabled

I’ve tested this with other code that does not require a parameter, and it worked. It would simply read something like: Sheets("sheet2").Shapes(1).OnAction = "someOtherMacro"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Needs quotes around the argument:

Sheets("sheet2").Shapes(1).OnAction = "'testIt """ & x & """'"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading