Home > Enterprise >  VBA Pass argument with .Onaction with parameter
VBA Pass argument with .Onaction with parameter

Time:03-26

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"

CodePudding user response:

Needs quotes around the argument:

Sheets("sheet2").Shapes(1).OnAction = "'testIt """ & x & """'"
  •  Tags:  
  • vba
  • Related