Hello,
I made a macro to create rectangles based on cell values for a 2D excel simulation. The code below adds the shapes once per each line I have in Sheet1:
Dim r As Long, s As Shape For r = 3 To Sheets("Sheet1").Cells(Rows.Count, 19).End(xlUp).Row If Sheets("Sheet1").Cells(r, 9).Value > 0 Then Set s = Sheets("AUTO").Shapes.AddShape(msoShapeRectangle, 410, 10, _ Sheets("Sheet1").Cells(r, "S"), _ Sheets("Sheet1").Cells(r, "T")) s.TextFrame2.TextRange.Text = _ Sheets("Sheet1").Cells(r, "E") & "; D=" _ & Sheets("Sheet1").Cells(r, "F") & "; L=" _ & Sheets("Sheet1").Cells(r, "G") & "; " _ & Sheets("Sheet1").Cells(r, "I") & " batches; additional pcs: " _ & Sheets("Sheet1").Cells(r, "J") Else
Column 9 (I) sets the number of shapes to be created. Its values may vary from 0 to 10.
So my need would be to integrate into the code above a condition so I could add the same shape multiple times based on the value on each row in column 9.
– if value in I3 is 1, it should add 1 shape of the type in row 3
– if value in I4 is 2, it should add 2 shapes of the type in row 4 and only then move on to the next row
– and so on
Thank you in advance.
message edited by Mrrrr
What this does is place a bunch of small rectangles on the Auto sheet based on the values in Sheet1 Column I, S and T.
I = Count of Rectangles Per Row
S, T = Size of Rectangle (I kept this small and consistant for testing purposes)
It will place rectangles in the same row as the Column I count. e.g. if I5 = 3, there will be 3 rectangles in Row 5 on the Auto sheet. In my example, it will place the 3 rectangles in Columns I:K, 1 per column.
In order for the rectangles to be placed “neatly” in each cell, the Row height on the Auto sheet must be the default of 15 and the Column width must be the default of 8.43.
For ease of testing, I started with this on Sheet1:
I ended up with this (x = Rectangle)
Here’s the code I used:
How To Post Data or Code —> Click Here Before Posting Data or VBA Code
message edited by DerbyDad03