简体   繁体   English

如何将使用参数的宏分配给 excel 中的按钮

[英]How to assign a macro that uses parameter to a button in excel

A client requested that I add the buttons as shown in image 1 below.客户要求我添加按钮,如下图 1 所示。

在此处输入图像描述

There can be from 1 to 100 sets of buttons.可以有 1 到 100 组按钮。 The reason is unimportant to this question.原因对于这个问题并不重要。

Seeing as they have practically identical functions I need to assign them but somehow have the row number available as a parameter or variable or something.看到它们具有几乎相同的功能,我需要分配它们,但不知何故,行号可用作参数或变量或其他东西。

Private Sub Edit_CVR_Row(Btn_row As Integer)

At the generation of the buttons the row number is part of a for loop and known and this is how I assign the macro在生成按钮时,行号是 for 循环的一部分并且是已知的,这就是我分配宏的方式

With btn
    .OnAction = "'Edit_CVR_Row """ & Btn_row & "'"
    .Caption = "Edit"
    .Name = "Sheet1_" + Str(Btn_row) + "_Edit"  
End With

This is the error I get这是我得到的错误

错误 1

There is a similar solution that uses the Evaluate function with similar results.有一个类似的解决方案使用 Evaluate function 得到类似的结果。

Any help will be greatly appreciated.任何帮助将不胜感激。

When you open your Excel file, is there a yellowish band on top of your cells asking you to activate the macros?当您打开 Excel 文件时,您的单元格顶部是否有一条黄色带,要求您激活宏? If so, you should accept it before running your macro, otherwise you will receive that kind of notification.如果是这样,您应该在运行宏之前接受它,否则您将收到那种通知。

It is also possible that macros are not activated in your Excel, you can check it by going in the File tab --> Options --> Trust center --> Macro setting, and then you will have to select the option you want.也有可能您的 Excel 中未激活宏,您可以通过进入文件选项卡 --> 选项 --> 信任中心 --> 宏设置来检查它,然后您将不得不 select 选择您想要的选项。

activesheet.buttons(application.caller).topleftcell.row activesheet.buttons(application.caller).topleftcell.row

Thank you for this Rory, it worked.谢谢你的这个罗里,它奏效了。

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

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