如何删除工作表上的所有按钮?

And*_*rry 8 excel vba excel-vba

我有一些代码在工作表上创建一个Save按钮(保存在wsReport变量中),但它不会删除以前的按钮.随着时间的推移,他们往往会积累起来.有没有办法做这样的事情?

wsReport.Buttons.All.Delete
Run Code Online (Sandbox Code Playgroud)

(显然不对,但是想一想我在寻找什么.)

Xsi*_*ial 13

见下面的代码:)

Sub RemoveButtons()
Dim i As Integer
    If ActiveSheet.ProtectContents = True Then
        MsgBox "The Current Workbook or the Worksheets which it contains are protected." & vbLf & "                          Please resolve these issues and try again."
    End If

    On Error Resume Next
        ActiveSheet.Buttons.Delete
End Sub
Run Code Online (Sandbox Code Playgroud)

资料来源:http: //www.mrexcel.com/forum/excel-questions/609668-delete-all-buttons-sheet-visual-basic-applications.html

或者你可以使用下面的代码:( VBA中的按钮位于Shapes集合中).

Sub DelButtons()
Dim btn As Shape

For Each btn In ActiveSheet.Shapes
    If btn.AutoShapeType = msoShapeStyleMixed Then btn.Delete
Next

End Sub
Run Code Online (Sandbox Code Playgroud)

source: 删除VBA按钮的集合