我有用焊缝标记的零件图。每张图都是相同的,但具有唯一的序列号。每个焊缝均由圆形 (msoShapeOval) 表示。在我点击保存功能后,我希望从绘图中删除所有圆形(并且仅圆形),以便我可以开始下一个序列号。
我尝试使用带有 Shape.Type 命令的 If 语句来删除所有 msoShapeOval,但该文件无法识别形状。
我还尝试了相反的方法,即“如果不是”(文本框、OLE、图片等)删除形状,但是删除了所有形状。
Sub DeleteAllWelds()
Dim shp As Shape
Set wb = ThisWorkbook
Set ws = wb.Sheets("Sheet1")
For Each shp In ws.Shapes
If shp.Type = msoShapeOval Then shp.Delete
Next shp
End Sub
这是调用DeleteWelds 子项的CommandButton
Private Sub CommandButton2_Click() 'Save and create new Weld Map for same Part Number
'Remove all weld shapes 'Reset any counters 'Reset fields in UserForm1 but not all
Set wb = ThisWorkbook
Set ws = wb.Worksheets("Sheet1")
Call SaveDoc
If UserForm3_Exit.Tag = "Go" Then
ws.Range("I2, I3, A6:K80").Value = ""
Call DeleteAllWelds
Unload Me
ElseIf UserForm3_Exit.Tag = "Cancel" Then
Unload Me
End If
结束子
最后是创建椭圆形的子。
Sub ShapeWithNum()
ActiveSheet.Shapes.AddShape(msoShapeOval, 20, 40, 20, 20).Select
Selection.ShapeRange.TextFrame2.TextRange.ParagraphFormat.Alignment = _
msoAlignCenter
Selection.ShapeRange.TextFrame2.VerticalAnchor = msoAnchorMiddle
Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Text = buttonCell
With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(1, 1). _
ParagraphFormat
.FirstLineIndent = 0
.Alignment = msoAlignCenter
End With
With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(1, 1).Font
.NameComplexScript = "+mn-cs"
.NameFarEast = "+mn-ea"
.Fill.Visible = msoTrue
.Fill.ForeColor.ObjectThemeColor = msoThemeColorLight1
.Fill.ForeColor.TintAndShade = 0
.Fill.ForeColor.Brightness = 0
.Fill.Transparency = 0
.Fill.Solid
.Size = 11
.name = "+mn-lt"
End With
End Sub
我只删除msoShapeOval的方法不会删除任何对象,但是当我使用If not(mso(插入我需要的))时,所有对象都会被删除。
最佳答案
您需要使用shp.AutoShapeType
而不是shp.Type
正如@AhmedAU 在评论中评论的那样(该评论现已删除)。
Excel 中有不同种类的形状。例如3D model
, AutoShape
, Callout
等等。您可以在 MsoShapeType enumeration (Office) 中获取整个列表。
所以当你说Shape.Type
时,那么它指的是MsoShapeType
。让我们举个例子。在工作表中插入这些形状
现在运行此代码
Sub Sample()
Dim ws As Worksheet
Dim Shp As Shape
Set ws = Sheet1
For Each Shp In ws.Shapes
Debug.Print "(" & Shp.Name & ")"; "---"; Shp.Type
Next Shp
End Sub
您将得到此输出
(Oval 1)--- 1
(TextBox 2)--- 17
(Straight Connector 4)--- 9
(Right Arrow 5)--- 1
(Explosion 1 6)--- 1
所以你会注意到Oval 1
, Right Arrow 5
和Explosion 1 6
形状类型为 1
。这意味着(如果您引用上面的链接)这些是自选图形
当您打印 msoShapeOval
的值时在立即窗口中使用 ?msoShapeOval
,您会注意到该值为 9
。绝对不是1
那么这个值是多少呢?这是Shape.AutoShapeType
值为 9
的属性对于 msoShapeOval
阅读Shape.AutoShapeType property (Excel) 。它表示返回或设置指定 Shape 或 ShapeRange 对象的形状类型,该对象必须表示除直线、自由图形或连接器之外的自选图形。
所以人们应该知道何时使用Shp.Type
以及何时使用Shp.AutoShapeType
现在试试这个代码
Sub Sample()
Dim ws As Worksheet
Dim Shp As Shape
Set ws = Sheet1
For Each Shp In ws.Shapes
Debug.Print "(" & Shp.Name & ")"; "--- Shape Type: "; Shp.Type; "--- AutoShape Type: "; Shp.AutoShapeType
Next Shp
End Sub
您将看到输出为
(Oval 1)--- Shape Type: 1 --- AutoShape Type: 9
(TextBox 2)--- Shape Type: 17 --- AutoShape Type: 1
(Straight Connector 4)--- Shape Type: 9 --- AutoShape Type: -2
(Right Arrow 5)--- Shape Type: 1 --- AutoShape Type: 33
(Explosion 1 6)--- Shape Type: 1 --- AutoShape Type: 89
因此,在您的代码中,只需更改
If shp.Type = msoShapeOval
至
If shp.AutoShapeType = msoShapeOval
关于excel - 使用 Excel VBA 命令删除除特定 ShapeType 之外的所有形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57914619/