如何从按钮调用宏并传递参数 [英] How to call a macro from a button and pass arguments
问题描述
我想在我的 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?
推荐答案
是的,您可以将宏分配给按钮(或其他 excel 控件/菜单操作)并将常量 OR 变量参数传递给它.
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'):
- 将宏名称用单引号括起来例如传递 2 个常量:
'Button1_Click("A string!", 7)'
- 为宏输入"字段选择此工作簿"
- 如果您希望传递变量(如单元格的值),请将参数括在 Evaluate() 中
例如,要将 Sheet1!$A$1
的值传递给按钮函数,您将在宏名称:"字段中包含以下文本:
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"))
如果您没有用评估"函数将变量参数括起来,excel 将返回错误公式太复杂,无法分配给对象.".
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屋!