如何从一个按钮调用一个宏并传递参数 [英] How to call a macro from a button and pass arguments
问题描述
按下按钮,有没有简单的方法将参数传递给宏?
是的,您可以将宏分配给按钮(或其他excel控件/菜单操作),并将常量OR变量参数传递给它。在分配宏窗口中(右键单击对象并选择分配宏):
- 将宏名称包含在单引号
中传递2个常量:'Button1_Click(A string!,7)' - 为宏在字段选择此工作簿
- 如果您希望传递变量(像单元格的值),请将参数包含在Evaluate()
例如,要将Sheet1!$ A $ 1的值传递给按钮功能,您将在宏名称:字段中具有以下文本:
'Button1_Click(Evaluate Sheet1!$ A $ 1))'
如果您没有使用Evaluate函数附加您的变量参数,则excel返回错误'公式太复杂被分配到一个对象'。
如果这是允许我的第一篇文章,我将包含一张图片。
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?
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.
这篇关于如何从一个按钮调用一个宏并传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!