形状安置和形状计数器

在VBA Excel中,我有几个关于形状的问题。

我有A列和B列。他们会像这样显示。

AB 1 | some text 1 to 5 shapes 2 | blabla OOOO 

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

另外,如果有人首先select了三个形状,并且想要将它改成四个形状,那么我必须把第四个形状放在正确的位置上。

所有这些必须仍然工作,如果我改变列的宽度。

我真的search了很多,但我似乎无法find一个正确的解决scheme。

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

 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