形状放置和形状计数器

Shape placement and shape counter

我对 VBA Excel 中的形状有一些疑问。

我有 A 列和 B 列。它们将像这样显示。

    A            B
1 | some text    1 to 5 shapes
2 | blabla       OOOO

我想通过用户窗体输入文本和形状。因此,如果用户选择“1”,我希望一个形状出现在 B 列单元格的左侧。如果用户选择“3”,我希望 3 个形状在 B 列中彼此相邻出现。

还有,如果有人一开始选的是3个造型,后来想改成4个造型,我就得把第四个造型放对了。

如果我更改列的宽度,所有这些都必须仍然有效。

我真的用谷歌搜索了很多,但我似乎找不到合适的解决方案。

这个位适合我,但当然你必须修改以使用你自己的形状和目的。

Sub DrawShapesInPlace(shapeCell As Range, numShapes As Integer, Optional gap As Double = 3#)
    Dim cellW As Double
    Dim cellH As Double
    Dim shapeW As Double
    Dim shapeUL As Double
    Dim shapeTop As Double
    Dim shapeH As Double
    Dim i As Integer
    Dim newShape As Shape

    If numShapes < 1 Then
        Exit Sub
    End If

    cellW = shapeCell.Width
    cellH = shapeCell.Height
    shapeW = (cellW / numShapes) - gap
    shapeUL = shapeCell.Left
    shapeTop = shapeCell.Top
    shapeH = cellH

    For i = 1 To numShapes
        Set newShape = ActiveSheet.Shapes.AddShape(msoShapeRectangle, _
                                                   shapeUL, _
                                                   shapeTop, _
                                                   shapeW, _
                                                   shapeH)
        newShape.Line.Weight = 1
        shapeUL = shapeUL + gap + shapeW
    Next i
End Sub

Sub DrawShapes()
    Call DrawShapesInPlace(ActiveSheet.Range("D9"), 3)
End Sub