简体   繁体   English

以编程方式将带有宏的按钮添加到Excel文件

[英]Programmatically add a button with a macro to an Excel file

I'm creating formated worksheets with an Excel VBA application and I would like to add programmatically add a button to each worksheet when created. 我正在使用Excel VBA应用程序创建格式化的工作表,我想在创建时以编程方式向每个工作表添加一个按钮。 This button must have a macro attached to it, which display the average of the values in a certain range in a special cell, and change the background color of this cell depending on the value of the average. 此按钮必须附加一个宏,该宏在特殊单元格中显示某个范围内某个值的平均值,并根据平均值来更改此单元格的背景色。 Is it possible to this, and if yes, how can I do it ? 这可能吗,如果可以,我该怎么办?

You can add the Button like this or example: 您可以像这样或示例添加按钮:

ActiveSheet.Buttons.Add(52.5, 7.5, 173.25, 41.25).Select 'Arguments are coordinates
Selection.OnAction = "Button_Click"

Now make sure the Button_Click sub has the logic you want. 现在,确保Button_Click子具有所需的逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM