Finance

Charts

Statistics

Macros

Search

All Shapes In Excel VBA

For better clarity, this section demonstrates all the different AutoShapes using the following VBA program:

Sub DisplayAllShapes()
    Dim Sh As Shape
    Dim i As Integer, lf As Integer, tp As Integer
    ' Select the worksheet
    ThisWorkbook.Worksheets("Sheet4").Activate
    ' Hide gridlines for clearer display
    ActiveWindow.DisplayGridlines = False
    ' Delete all existing shapes
    For Each Sh In ActiveSheet.Shapes
        Sh.Delete
    Next Sh
    ' Initial position values
    lf = 5
    tp = 5
    ' Create all possible shapes
    For i = 1 To 137
        Set Sh = ActiveSheet.Shapes.AddShape(i, lf, tp, 30, 30)
        ' Format the shape
        With Sh
            .Line.Weight = 1
            .Line.ForeColor.RGB = RGB(0, 0, 0)
            .Fill.ForeColor.RGB = RGB(255, 255, 255)
        End With
        ' Add the shape type number as text inside the shape
        With Sh.TextFrame.Characters
            .Font.Color = vbBlack
            .Font.Size = 7
            .Text = i
        End With
        ' Calculate position for next shape
        lf = lf + 35
        If i Mod 15 = 0 Then
            lf = 5
            tp = tp + 35
        End If
    Next i
    Set Sh = Nothing
End Sub

Explanation:
First, gridlines on the worksheet are hidden to improve visibility. Then, any existing shapes on the sheet are deleted.

Initial coordinates for placing the AutoShapes are set.

Within the loop, all 137 different AutoShapes are created using the AddShape() method.

Each shape is sized 30 by 30 points, drawn with a thin black border and a white fill.

Inside each shape, its Type number (the current loop index) is displayed as text.

At the end of each loop iteration, the position for the next shape is calculated, moving horizontally by 35 points and moving down a row every 15 shapes.

The Figure shows a small excerpt of the shapes. Depending on the shape’s design, the type number may only be partially visible, but can always be inferred by the neighboring shapes.

0 0 votes
Évaluation de l'article
S’abonner
Notification pour
guest
0 Commentaires
Le plus ancien
Le plus récent Le plus populaire
Online comments
Show all comments
Facebook
Twitter
LinkedIn
WhatsApp
Email
Print
0
We’d love to hear your thoughts — please leave a commentx