简体   繁体   English

如何使用 VBA 删除工作表中的特定形状?

[英]how to delete a specific shape in a sheet using VBA?

I have a sheet with a lot of shapes.我有一张有很多形状的床单。 I need to delete only the shapes which are located after row 15, and shapes which have Resize and Clear All written on it.我只需要删除位于第 15 行之后的形状,以及上面写有 Resize 和 Clear All 的形状。 The shapes are a rectangle: rounded corners keeping text and having a macro assigned形状是一个矩形:圆角保留文本并分配了宏

例如

How you can see in the image, I will have pictures (screenshots) and it cant be deleted, the thing I am trying to delete is the blue "button".您如何在图像中看到,我将有图片(屏幕截图)并且无法删除,我要删除的东西是蓝色的“按钮”。

First, your If-statement is wrong, see BigBen's comment:首先,您的 If 语句是错误的,请参阅 BigBen 的评论:

If oShape.Name = "Resize" Or oShape.Name = "Clear All" Then

However, this will check the name of the shapes, not their text.但是,这将检查形状的名称,而不是它们的文本。 To get the text of the shape, you can use oShape.TextFrame2.TextRange.Text .要获取形状的文本,您可以使用oShape.TextFrame2.TextRange.Text However, you can face two small issues with that:但是,您可能会面临两个小问题:

(1) There are shapes without text, eg Pictures. (1) 有没有文字的形状,例如图片。 This can be checked with oShape.TextFrame2.HasText这可以用oShape.TextFrame2.HasText检查

(2) It may be the case that the text has a newline at the end or that it has contains leading or trailing spaces, so I would suggest you write the content into a variable and use the Instr -function: (2) 可能是文本末尾有换行符,或者它包含前导或尾随空格,因此我建议您将内容写入变量并使用Instr -function:

if oShape.TextFrame2.HasText Then
    dim shapeText as string
    shapeText = oShape.TextFrame2.TextRange.Text
    if InStr(shapeText, "Resize") > 0 or InStr(shapeText, "Clear All") > 0 then
       oShape.Delete
    End If
End If

Try the next code, please (now it deletes only rounded corners rectangles according to your conditions):请尝试下一个代码(现在它根据您的条件仅删除圆角矩形):

Sub testDeleteInsertedShapes()
Dim ws As Worksheet, sh As Shape, shR As ShapeRange, rng As Range
  Set ws = ActiveSheet
  Set rng = ws.Range(ws.Range("A1"), ws.Cells(15, Columns.count))

  Application.EnableEvents = False
  For Each sh In ws.Shapes
    If sh.Type = 1 Then 'rounded rectangles
        If Not Intersect(sh.TopLeftCell, rng) Is Nothing Then
            If sh.TextFrame2.TextRange.text = "Resize" Or _
                 sh.TextFrame2.TextRange.text = "Clear All" Then
                sh.Delete
            End If
        Else
            sh.Delete
        End If
    End If
  Next
  Application.EnableEvents = True
End Sub

And a code version deleting all shapes type (excepting pictures), I start working before you answered my question:还有一个删除所有形状类型(图片除外)的代码版本,我在你回答我的问题之前就开始工作了:

Sub deleteShapesAllTypes()
  Dim ws As Worksheet, sh As Shape, shR As ShapeRange, rng As Range
  Set ws = ActiveSheet
  Set rng = ws.Range(ws.Range("A1"), ws.Cells(15, Columns.count))
  Debug.Print rng.Address
  For Each sh In ws.Shapes
    If sh.Type = 8 Then
        If Not Intersect(sh.TopLeftCell, rng) Is Nothing Then
            If sh.OLEFormat.Object.text = "Resize" Or _
                   sh.OLEFormat.Object.text = "Clear All" Then
                sh.Delete
            End If
        Else
            sh.Delete
        End If
    ElseIf sh.Type = 12 Then
        If Not Intersect(sh.TopLeftCell, rng) Is Nothing Then
            If sh.OLEFormat.Object.Object.Caption = "Resize" Or _
                   sh.OLEFormat.Object.Object.Caption = "Clear All" Then
                sh.Delete
            End If
        Else
            sh.Delete
        End If
    Else
        If sh.Type <> 13 Then
            If Not Intersect(sh.TopLeftCell, rng) Is Nothing Then
                If sh.TextFrame2.TextRange.text = "Resize" Or _
                     sh.TextFrame2.TextRange.text = "Clear All" Then
                    sh.Delete
                End If
            Else
                sh.Delete
            End If
        End If
    End If
  Next
End Sub

Try:尝试:

Sub shapeKiller()
    Dim i As Long, N As Long, nm As String, rw As Long
    Dim sh As Shape
    N = ActiveSheet.Shapes.Count

    For i = N To 1 Step -1
        Set sh = ActiveSheet.Shapes(i)
        nm = sh.Name
        rw = sh.TopLeftCell.Row
        If nm = "Resize" Or nm = "Clear All" Or rw > 15 Then
            sh.Delete
        End If
    Next i
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM