删除VBA中除复选框以外的所有对象

时间:2014-03-18 01:49:51

标签: excel vba object excel-vba checkbox

我有一张包含一些表格控制按钮,表格控制复选框和奇怪形状的表格。我目前有一个宏从表单中删除所有对象。 (ActiveSheet.DrawingObjects.Delete) 我想做的就是删除所有对象,除了复选框。 这可能吗?如果是这样我怎么去呢?

2 个答案:

答案 0 :(得分:0)

尝试使用图纸模块

Sub test()

For Each DrawingObject In ActiveSheet.DrawingObjects
    If TypeName(DrawingObject) <> "CheckBox" Then DrawingObject.Delete
Next

End Sub

答案 1 :(得分:0)

'Delete only specific shapes

'What if you only want to delete control toolbox controls, Pictures or forms controls.
'You can loop through the collection and check the Type of the control.

'12 = ActiveX control (control toolbox) or a linked or embedded OLE object.
'13 = Picture
'8 = Forms controls

'For Type 8 we use another macro to avoid the problem of losing AutoFilter and Data Validation dropdowns on your worksheet.See the example in this section "Delete only Forms controls"

Sub Shapes2()
'Loop through the Shapes collection and use the Type number of the control
    Dim myshape As Shape
    For Each myshape In ActiveSheet.Shapes

    ' ActiveX control (control toolbox) or a linked or embedded OLE object

    If myshape.Type = 12 Then myshape.Delete

    ' You can also use  myshape.Visible = False

    Next myshape
'End Sub If you want to know all the Type numbers of all controls on your worksheet you can run this macro to add a new worksheet with the names and Type numbers of all objects on your worksheet.You can find the number then that you must use in the code to delete the objects you want.

Sub ListAllObjectsActiveSheet()

    Dim NewSheet As Worksheet
    Dim MySheet As Worksheet
    Dim myshape As Shape
    Dim I As Long

    Set MySheet = ActiveSheet
    Set NewSheet = Worksheets.Add

    With NewSheet
        .Range("A1").Value = "Name"
        .Range("B1").Value = "Visible(-1) or Not Visible(0)"
        .Range("C1").Value = "Shape type"
        I = 2

        For Each myshape In MySheet.Shapes
            .Cells(I, 1).Value = myshape.Name
            .Cells(I, 2).Value = myshape.Visible
            .Cells(I, 3).Value = myshape.Type
            I = I + 1
        Next myshape

        .Range("A1:C1").Font.Bold = True
        .Columns.AutoFit
        .Range("A1:C" & Rows.Count).Sort Key1:=Range("C1"), _
                    Order1:=xlAscending, Header:=xlYes
    End With

End Sub