Delete all Excel drawing layer shapes that match the template

Consider an Excel spreadsheet with many forms. To select two forms:

ActiveSheet.Shapes.Range(Array("Freeform 314", "Freeform 278")).Select

The goal is to remove all shapes, starting with "Freeform".

I found an example referencing MS VBScript Regular Expression 5.5 , but forms are not on specific cells. The number or a certain number of figures is almost not shown. So the ugly solution may be the brute force of the string names, on the other hand, I hope you can help me achieve an elegant solution.

The code below does not work, but you can better understand the problem.

Sub DeleteShapes()

    Dim re As New RegExp
    re.Pattern = "Freeform*"
    Dim cell As Range
    For Each cell In ActiveSheet.Shapes.Range(Array(re.Pattern)).Select
       Selection.delete
    Next cell
End Sub
+3
source share
2 answers

, ,

Dim shape as Variant
For each shape in ActiveSheet.Shapes
   If instr(1, shape.Name, "Freeform") <> 0 then shape.delete
Next shape

, . , , "Freeform" .

, "Freeform", : instr (1, shape.Name, "Freeform" ) < > 0 : instr (1, shape.Name, "Freeform" ) = 1

+3

, Freeform.

Dim shape As Excel.shape

For Each shape In ActiveSheet.Shapes

    If Left(shape.Name, 8) = "Freeform" Then
        shape.Delete
    End If

Next
+4

Source: https://habr.com/ru/post/1778385/


All Articles