我已将以下三个buttons插入到我的Excel电子表格中:

我使用的是德语Excel版本:Schaltfl che= Button
如您所见,buttons由1,2,3计数,每增加一个button就会得到下一个更高的计数号。
现在,我运行以下VBA来删除所有buttons
Sub Delete_Buttons()
Sheet1.Select
Sheet1.Buttons.Delete
End Sub运行此VBA后,再次插入buttons:

如您所见,buttons是从3开始计算的。
如何重新设置按钮的计数并使计数再次从1开始?
发布于 2020-01-17 13:56:31
解决这一问题的最佳办法是:
SubDelete_Buttons()Save 1.选择Sheet1.Buttons.Delete End Sub
(现在开始从Button 1开始计数)
发布于 2019-10-11 06:35:31
或者您也可以使用一个通过其Left属性“命令”按钮文本的子
此示例使用SortedList对象。
Sub Rename_Buttons()
With CreateObject("System.Collections.SortedList")
Dim btn As Button
For Each btn In Sheet1.Buttons
.Add btn.Left, btn
Next
Dim j As Long
For j = 0 To .Count - 1
.GetValueList()(j).Text = "Button" & j + 1
Next
End With
End Sub发布于 2019-10-11 08:27:16
您还可以使用:
Sub create_new()
Dim counter As Long, MaxValue As Long, i As Long
Dim btn As Button, btnNew As Button
Dim FullBtnName As String
counter = 3
MaxValue = 0
'You could change sheet name
With ThisWorkbook.Worksheets("Sheet1")
'Loop all butons in the sheet
For Each btn In .Buttons
'Split the name to get its number
FullBtnName = btn.Name
If CInt(Split(FullBtnName, " ")(1)) > MaxValue Then
'Get the max number
MaxValue = CInt(Split(FullBtnName, " ")(1))
End If
Next btn
'Loop 3 times to create 3 shhets
For i = 1 To 3
Set btnNew = .Buttons.Add(.Range("A" & i + 5).Left, .Range("A" & i + 5).Top, .Range("A" & i + 5).Width, .Range("A" & i + 5).Height)
'Change button name & caption
With btnNew
.Caption = "Button " & MaxValue + i
.Name = "Button" & MaxValue + i
End With
Next i
End With
End Subhttps://stackoverflow.com/questions/58335249
复制相似问题