Adding parameters to the OnAction property of Excel buttons

4 Posted by
Posted in Programming

Sometimes you need to pass parameters via a button in Excel but the standard OnAction property syntax does not allow this. If you try .OnAction=”myProc(99,’Some text’)” you will receive an error. But there is a way to get around this.

You can add a parameter to the OnAction property of the button. If you had a procedure like this,

Sub myProc(id As Integer, myText As String)
MsgBox (id & " " & myText)
End Sub

you can set the OnAction property of a button as follows:

sheet1.Shapes(1).OnAction = "'myProc 99,""Some text""'"

Careful with the quotes and double quotes, you need single quotes around the entire procedure call and parameters, you need repeated double quotes around strings. Separate parameters with commas.

Tagged: , ,

4 thoughts on “Adding parameters to the OnAction property of Excel buttons

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>