Excel VBA形成内部名称/types
我需要删除除命令button之外的所有形状。 或者只删除椭圆,直线和画线。
Sub deleteShapes() Dim shp As Shape For Each shp In ActiveSheet.Shapes shp.Delete Next shp End Sub
在这个答案杰米·布尔删除形状:
If Not (Shp.Type = msoOLEControlObject Or Shp.Type = msoFormControl) Then Shp.Delete
但是我怎么能得到我的命令buttontypes? 还是其他的对象types? 我试过了
Sub testShapes() Dim shp As Shape For Each shp In ActiveSheet.Shapes MsgBox (shp.Type) Next shp End Sub
但它只给出数字:9,5,1,12。不知道哪个数字是哪个形状。 有没有办法得到像msoOLEControlObject
这样的内部名称,或者至less要确保编号1是真正的命令button?
types列表在这里: https : //msdn.microsoft.com/en-us/VBA/Office-Shared-VBA/articles/msoshapetype-enumeration-office所有的值在VBA中定义为常量,所以你可以写
if not shp.Type = msoOLEControlObject then shp.Delete end if
要获得更多有关您拥有什么样的控制的信息:
Dim sh As Shape For Each sh In Activesheet.Shapes Debug.Print sh.Name, sh.Type If sh.Type = msoFormControl Then Debug.Print " msoFormControl:" & sh.FormControlType End If If sh.Type = msoOLEControlObject Then Debug.Print " msoOLEControlObject: " & TypeName(sh.OLEFormat.Object.Object) End If Next sh
FormControlType在这里显示: https : //msdn.microsoft.com/en-us/vba/excel-vba/articles/xlformcontrol-enumeration-excel – 所有也被定义为VBA常量
如果您正在使用形状的默认名称,然后对于窗体button:
Sub poiuyt() Dim shp As Shape For Each shp In ActiveSheet.Shapes If Left(shp.Name, 6) = "Button" Then Else shp.Delete End If Next shp End Sub
如果该button是activex,则:
Sub trewq() Dim shp As Shape For Each shp In ActiveSheet.Shapes If Left(shp.Name, 13) = "CommandButton" Then Else shp.Delete End If Next shp End Sub
只有在名称是默认types的情况下,此方法才有效。