如果您使用此,它会给你一些功能集成到更动态命名的按钮。但是,由于您正在使用ActiveCell来放置它,所以如果您尝试一次创建它们,则会导致它们的所有重叠问题。如果你能详细说明按钮放置的位置,如果它们相同,或者决定了它们的位置,我可以给出更多细节。
Dim lCount As Long
lCount = 1
Set newButton = ActiveSheet.Buttons.Add(_
ActiveCell.Left, _
ActiveCell.Top, _
ActiveCell.Offset(0, 1).Range("A1:C2").Width, _
ActiveCell.Offset(0, 1).Range("A1:A3").Height)
With newButton
.Name = "Button" & lCount 'The actual button name.
.Caption = newButton.Name 'This is the DISPLAY on the button.
End With
另一种方法是省略lCount并只在代码中命名按钮。这将涉及到激活所需的单元,然后运行代码,并将.Name属性中的值从1更改为2,3等。
With newButton
.Name = "Button1" 'The actual button name.
.Caption = newButton.Name 'This is the DISPLAY on the button.
End With
你可以有一个隐藏的工作表(例如,在“HiddenHelper”),其跟踪的您已经创建多少按键,并有lCount指的是电池。当您清除表,您重置价值,以及回到1。然后,当你添加一个按钮,都将代码添加至单元格值保持跟踪你有多少这样使用:
Dim lCount As Long
lCount = Sheets("HiddenHelper").Range("A1")
Set newButton = ActiveSheet.Buttons.Add(_
ActiveCell.Left, _
ActiveCell.Top, _
ActiveCell.Offset(0, 1).Range("A1:C2").Width, _
ActiveCell.Offset(0, 1).Range("A1:A3").Height)
With newButton
.Name = "Button" & lCount 'The actual button name.
.Caption = newButton.Name 'This is the DISPLAY on the button.
End With
Sheets("HiddenHelper").Range("A1") = Sheets("HiddenHelper").Range("A1") + 1
当然,您可以随心所欲地命名助手表,并将按钮数的范围设置为适合您的任何范围。