删除VBA中除复选框以外的所有对象
我有一个表,其中包含一些表单控件按钮、表单控件复选框和奇数形状。我目前有一个宏,可以从工作表中删除所有对象。(ActiveSheet.DrawingObjects.Delete) 我想做的是让它删除除复选框之外的所有对象。删除VBA中除复选框以外的所有对象,vba,object,excel,checkbox,Vba,Object,Excel,Checkbox,我有一个表,其中包含一些表单控件按钮、表单控件复选框和奇数形状。我目前有一个宏,可以从工作表中删除所有对象。(ActiveSheet.DrawingObjects.Delete) 我想做的是让它删除除复选框之外的所有对象。 这可能吗?如果是这样的话,我该怎么做呢?试试工作表模块 Sub test() For Each DrawingObject In ActiveSheet.DrawingObjects If TypeName(DrawingObject) <> "Chec
这可能吗?如果是这样的话,我该怎么做呢?试试工作表模块
Sub test()
For Each DrawingObject In ActiveSheet.DrawingObjects
If TypeName(DrawingObject) <> "CheckBox" Then DrawingObject.Delete
Next
End Sub
子测试()
对于ActiveSheet.DrawingObjects中的每个DrawingObject
如果TypeName(DrawingObject)“复选框”,则DrawingObject.Delete
下一个
端接头
您有试用过的代码吗?如果有,请把它贴在这里。
'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