Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a macro from a button and pass arguments

Tags:

excel

vba

I want to add a button to my excel worksheet which should call a macro that can handle one agument (an integer value). Sadly when creating the button, I cannot link any macro that has arguments. Also just typing the macro and the argument does not work.

Is there any simple solution to pass an argument to a macro when a button is pressed?

like image 241
user3470049 Avatar asked Apr 24 '15 21:04

user3470049


People also ask

How do I call a macro from a button?

Click the worksheet location where you want the upper-left corner of the button to appear. The Assign Macro popup window appears. Note: If you have already inserted a button, you can right-click on it, and select Assign Macro. Assign a macro to the button and click OK.

How can you pass arguments to a macro?

A parameter can be either a simple string or a quoted string. It can be passed by using the standard method of putting variables into shared and profile pools (use VPUT in dialogs and VGET in initial macros). This method is best suited to parameters passed from one dialog to another, as in an edit macro.

How do I link a macro to a button?

Click File > Options > Quick Access Toolbar. In the Choose commands from list, click Macros. Select the macro you want to assign a button to. Click Add to move the macro to the list of buttons on the Quick Access Toolbar.

How do I call a macro from a parameter in Excel?

Yes, you can assign a macro to a button (or other excel controls/menu actions) and pass constant OR variable arguments to it. In the 'Assign Macro' window (right-click on object and select 'Assign Macro'): Enclose the macro name in single quotes e.g. to pass 2 constants: 'Button1_Click("A string!", 7)'


Video Answer


2 Answers

Yes, you can assign a macro to a button (or other excel controls/menu actions) and pass constant OR variable arguments to it.

In the 'Assign Macro' window (right-click on object and select 'Assign Macro'):

  • Enclose the macro name in single quotes e.g. to pass 2 constants: 'Button1_Click("A string!", 7)'
  • Select 'This Workbook' for the 'Macros in' field
  • If you wish to pass a variable (like the value of a cell), enclose the parameter in Evaluate()

For example, to pass the value of Sheet1!$A$1 to a button function, you would have the following text in the 'Macro name:' field:

Button1_Click(Evaluate("Sheet1!$A$1")) 

If you don't enclose your variable argument with an 'Evaluate' function, excel returns the error 'Formula is too complex to be assigned to an object.'.

I would have included an image if this were allowed on my first post.

like image 154
QA Collective Avatar answered Sep 19 '22 15:09

QA Collective


Suppose you have a public sub take 1 argument like below (just for explanation purposes.)

Macro

And you insert a button on Worksheet like below, and you can not find the macro name when you want to assign your sub to this button.

Button

Now, you can type in your sub name + space + argument manually in single quotes, like below, click ok.

Type macro name

Then you see, problem solved.

Result

like image 21
AntiqueWhale Avatar answered Sep 20 '22 15:09

AntiqueWhale