Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel, VBA: How to pass multiple variables to .OnAction

Tags:

excel

vba

I am trying to pass multiple variables to a .OnAction call for a button. I have not been able to find answers that relate to multiple variables, and I can do it with just one variable. Here is what I have that works for one variable:

shpBar.OnAction = "'Button_Click """ & strText & """'"

How can I add two other variables to this (such as VarA and VarB)?

like image 305
MikeG Avatar asked Oct 26 '12 01:10

MikeG


People also ask

How can you pass variable to VBA functions?

VBA allows you to pass variables into subroutines and functions in two ways. You can specify either ByVal or ByRef for each of the variables that are passed in. The ByVal and ByRef distinction for subroutine and function parameters is very important to make. In VBA all objects are passed by reference.

Can you assign variables in VBA?

Setting variables In many programming languages, you can assign a value to the variable in the same statement that you use to declare it. In VBA, declaring variables is optional, but you can't declare AND set the variable at the same time. We've added a line that assigns the value of a variable.

How do I pass a macro to a variable in Excel?

We must type in the name of the macro and then also the value for the argument. Type in the name of the macro and then a space and then the value that you want to send to the macro. Note: the text is surrounded by double quotation marks and then the entire thing is surrounded by single quotation marks.

How do I pass a variable in Excel?

Pass Text Values: Let's send a text value to the macro. When you send a text value, you must always put double quotation marks around it. Note: when you pass a value using the Call method, the second example, you must put the values within parentheses.


1 Answers

Assuming that VarA and VarB are variants or numeric variables, this would work:

.OnAction = "'Button_Click """ & strText & """," & varA & "," & varB & " '"

If they are strings, you need to add two more double-quotes around each variable name.

.OnAction = "'Button_Click """ & strText & """,""" & varA & """,""" & varB & """ '"
like image 140
Doug Glancy Avatar answered Oct 02 '22 15:10

Doug Glancy