我在Excel中使用表单按钮,
当我添加表单按钮时,它们会添加名称Button 1, Button 2, Button 3
......
清除工作表并删除所有按钮后,当我再次添加它们时,我希望创建Button 1
而不是Button 4
。
那可能吗?如果是这样,如何实现呢?
提前致谢
我用来创建按钮的代码
ActiveSheet.Buttons.Add( _
ActiveCell.Left, _
ActiveCell.Top, _
ActiveCell.Offset(0, 1).Range("A1:C2").Width, _
ActiveCell.Offset(0, 1).Range("A1:A3").Height)
答案 0 :(得分:0)
如果您使用它,它将为您提供一些更动态地命名按钮的功能。但是,由于您使用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.然后当您添加另一个按钮时,请将代码添加到单元格值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
当然,您可以根据需要为帮助表命名,并将按钮计数的范围设置为适合您的任何一种。