简体   繁体   English

使用VBA以编程方式向Excel工作表中添加几个按钮

[英]Programmatically add several buttons to an Excel worksheet with VBA

I managed to programmatically add a button to a worksheet with an associated macro, but I'm having a problem when I try to add more buttons with associated macros, here is the code I use : 我设法以编程方式向带有关联宏的工作表中添加了一个按钮,但是当我尝试向关联的宏添加更多按钮时遇到了问题,这是我使用的代码:

'Adding the first button
 With newWorkBook.Worksheets(1).Buttons
    .Add 350, 15, 173.25, 41.25
    .Caption = "Exporter"
    .OnAction = "'" & ThisWorkbook.FullName & "'!export_Click_FCM"
 End With

 'Adding the second button
 With newWorkBook.Worksheets(1).Buttons
    .Add 350, 66.25, 173.25, 41.25
    .Caption = "Ouvrir le fichier Export"
    .OnAction = "'" & ThisWorkbook.FullName & "'!open_export_FCM"
 End With

When using this code, the first button appears with the caption of the second one and with its macro associated, and the second button appears with the default caption "button2" and with no macro associated, how can I solve this problem? 使用此代码时,第一个按钮与第二个按钮的标题一起出现,并且与它的宏相关联,而第二个按钮与默认标题“ button2”一起显示,并且没有宏相关联,我该如何解决这个问题?

Your first two lines of each block should be one: 每个块的前两行应为:

With newWorkBook.Worksheets(1).Buttons.Add(350, 15, 173.25, 41.25)

so you get a reference to the added Button , not to the Buttons collection. 因此您可以引用添加的Button ,而不是Buttons集合。

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

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