Passing multiple arguments to a subroutine using the .onaction command in a menuitem

LD2000

New Member
Joined
Sep 30, 2018
Messages
3
I am trying to pass 2 arguments to a subroutine by assigning it to a .onaction command. The code works when I only pass one string argument but falls down ( I get the "The macros is not available in this workbook or all macros are disabled" error) when I try and add a second integer argument.

The code that works is:-
.OnAction = "'PopulateFields(""" & myArray(x) & """)'"

The subroutine in this instance is declared as:-
PopulateFields(strAppType As String)

The failing code looks like this:-
.OnAction = "'PopulateFields(""" & myArray(x) & """, " & 3 & ")'"
PopulateFields(strAppType As String, intTier As Integer)

I have checked the value of the text passed to the .onaction command in the immediate window and it looks like this:-
'PopulateFields("Screed And Overscreed", 3)'

In the instance that works it looks like this
'PopulateFields("Screed And Overscreed")'

Can anyone see a problem??
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
You should not be using brackets.
 
Upvote 0
Hi,

welcome to forum

try removing the Parentheses (brackets)

not tested

Code:
 .OnAction = "'PopulateFields """ & myarray(x) & """, " & 3 & " '"

Dave
 
Upvote 0
Thank you Dave and Rory, that did it.

I don't suppose one of you knows why it worked with parentheses with the single string argument but not once I added a second argument? Not critical but interested to know.

LD
 
Upvote 0
Thank you Dave and Rory, that did it.

I don't suppose one of you knows why it worked with parentheses with the single string argument but not once I added a second argument? Not critical but interested to know.

LD

if you use the Call keyword to call a procedure that passes argument(s) then argument list must be enclosed in parentheses. If not, then you omit the parentheses around list.

As you are assigning the macro to OnAction you exclude parentheses however, I am little puzzled why it worked as you say for single argument and have no definitive answer to your question and if find time today, will have a play but maybe Rory as a better idea for this.

Dave
 
Upvote 0
Without the Call keyword, if you use brackets round arguments, VBA tries to evaluate the contents of the brackets as an expression, then pass the result to the routine. Evaluating a single argument is ok (unless it’s an object) since VBA will evaluate say (5) or (“hello”) as simply those values, but trying to evaluate (5, “hello”) as one expression makes no sense to VBA.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,898
Messages
6,175,272
Members
452,628
Latest member
dd2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top