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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35409343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 10:13:22  来源:igfitidea点击:

How do I delete all buttons on a worksheet?

excel-vbavbaexcel

提问by Andrew Perry

I've got a bit of code that creates a Save button on a worksheet (held in the wsReportvariable), but it doesn't remove previous buttons. Over time, they tend to build up. Is there any way to do something like this?

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

wsReport.Buttons.All.Delete

(Not right, obviously, but gives an idea of what I'm looking for.)

(显然不正确,但可以让我了解我在寻找什么。)

回答by XsiSec

See code below :)

见下面的代码:)

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

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

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

or could you use code below: (Buttons in VBA are in the Shapes collection).

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

Sub DelButtons()
Dim btn As Shape

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

End Sub

source: Deleting a collections of VBA buttons

来源: 删除一组 VBA 按钮

回答by Info City

See the code below:

请参阅下面的代码:

Sub DeleteAllShapes() 

    ActiveSheet.Shapes.SelectAll

    Selection.Delete

End Sub

回答by thatguy

There's an easier method that doesn't use VB:

有一个不使用 VB 的更简单的方法:

  1. Click the Developer tab
  2. Click Design Mode
  3. Click on any button in the worksheet
  4. Press Ctrl + A to select all buttons in the worksheet
  5. Press Delete
  1. 单击开发人员选项卡
  2. 单击设计模式
  3. 单击工作表中的任意按钮
  4. 按 Ctrl + A 选择工作表中的所有按钮
  5. 按删除