使用VBA以编程方式向Excel工作表中添加几个按钮 [英] Programmatically add several buttons to an Excel worksheet with VBA

查看:100
本文介绍了使用VBA以编程方式向Excel工作表中添加几个按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设法以编程方式将按钮添加到带有关联宏的工作表中,但是当我尝试添加更多按钮与关联宏时遇到问题,这是我使用的代码:

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

使用此代码时,第一个按钮与第二个按钮的标题一起出现,并且与它的宏相关联,而第二个按钮与默认标题"button2"一起显示,并且没有宏相关联,我该如何解决此问题?

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?

推荐答案

每个块的前两行应为一:

Your first two lines of each block should be one:

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

因此您可以引用添加的 Button ,而不是 Buttons 集合.

so you get a reference to the added Button, not to the Buttons collection.

这篇关于使用VBA以编程方式向Excel工作表中添加几个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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