computing
  • 8

Solved VBA To Generate Multiple Shapes Based On Number In Cell

  • 8

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

Share

1 Answer

  1. OK, even though I’m not sure if this is what you want, I think your answer may be buried within this code.

    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              S        T
    1
    2
    3       1              3        5
    4       2	       3        5
    5       3	       3        5
    6       	       3        5
    7       2	       3        5
    8       5	       3        5
    9       1	       3        5
    10      2	       3        5
    11      3	       3        5
    

    I ended up with this (x = Rectangle)

             I       J        K        L       M  
    1
    2
    3       x	
    4       x        x	
    5       x        x        x
    6  
    7       x        x	
    8       x        x        x       x        x
    9       x
    10      x        x	
    11      x        x        x
    

    Here’s the code I used:

    Sub RecPlacer()
    Dim s As Shape
    Dim r As Long, h As Long, v As Long
    
    '****************************************************
    'Clear Shapes For Testing
        Sheets("AUTO").DrawingObjects.Delete      '<----------
    '****************************************************
        
    'Initialize Column Placement Variable (I)
        h = 346
    
    'Initialize Rown Placement Variable (2)
        v = 20
    
    'Loop through Column I
         For r = 3 To Sheets("Sheet1").Cells(Rows.Count, 19).End(xlUp).Row
    
    'Increment Row Placement Variable By One Row
          v = v + 15
             
    'Add Rectangles Based On Value In Column Sheet1 Collumn I
            If Sheets("Sheet1").Cells(r, 9).Value > 0 Then
              For r_count = 1 To Sheets("Sheet1").Cells(r, 9).Value
                
    'Increment Column Variable By One Column
                h = h + 48.5
            
    'Place Rectangles & Text
                  Set s = Sheets("AUTO").Shapes.AddShape(msoShapeRectangle, h, v, _
                          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")
              Next
            
    'Reset Column Placement Variable (I)
              h = 346
            
            End If
         Next
    End Sub

    How To Post Data or Code —> Click Here Before Posting Data or VBA Code

    message edited by DerbyDad03

    • 0