如何从一个按钮调用一个宏并传递参数 [英] How to call a macro from a button and pass arguments

查看:206
本文介绍了如何从一个按钮调用一个宏并传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想添加一个按钮到我的excel工作表,它应该调用一个宏,可以处理一个文件(一个整数值)。遗憾的是,创建按钮时,我无法链接任何具有参数的宏。另外也只是键入宏,参数不起作用。



按下按钮,有没有简单的方法将参数传递给宏?

解决方案

是的,您可以将宏分配给按钮(或其他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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆