VBA: Run loop to create shape

ceecee88

Board Regular
Joined
Jun 30, 2022
Messages
59
Office Version
  1. 365
  2. 2013
Platform
  1. Windows
Hi, I am trying to create a loop that run through each organization level and create the shape accordingly. Currently I only get it to create the first shape but it won't run through and create the rest of it. For example if I have 3 people on level 2 it create 1 shape and stop. Below is the code I have so far, any help will be much appreciated.

1676695172335.png

1676695124606.png


VBA Code:
  Level = 2
    Staff.Range("M3").Value = Level
   Staff.Range("A2:H" & LastRow).AdvancedFilter xlFilterCopy, CriteriaRange:=Staff.Range("M2:M3"), CopyToRange:=Staff.Range("P2:W2"), Unique:=True
    LastResultRow = Staff.Range("P99999").End(xlUp).Row
    If LastResultRow < 3 Then GoTo NoResults
    Staff.Range("X3:X" & LastResultRow).Formula = Staff.Range("X1").Formula
    'Sort based on underlings
    With Staff.Sort
        .SortFields.Clear
        .SortFields.Add Key:=Staff.Range("X3"), SortOn:=xlSortOnValues, Order:=xlDescending 'Sort Supervisor Underlings
        .SortFields.Add Key:=Staff.Range("T3"), SortOn:=xlSortOnValues, Order:=xlDescending 'Sort Staff Underlings
        .SetRange Staff.Range("P3:X" & LastResultRow)
        .Apply
    End With
   
    LevelQty = LastResultRow - 2
    If Level = 2 Then '2nd Level (fist sub-row)
        LeftPos = CentPos - ((((ShapeSpacer) * (LevelQty - 1)) + ShapeWidth) / 2)
        PrevQty = LevelQty 'Save Previous Level Qty
        PrevLeft = LeftPos
    Else 'Remaining rows 'Add additonal spacers
        LeftPos = CentPos - (((((ShapeWidth / 3) + ShapeSpacer) * (LevelQty - 2)) + ShapeWidth) / 2)
        If PrevQty >= LevelQty And PrevQty <> 0 Then LeftPos = PrevLeft
        PrevQty = LevelQty 'Save Previous Level Qty
        PrevLeft = LeftPos
    End If
            LevelNumb = 1
            StaffID = Staff.Range("P" & LevelNumb + 2).Value
            StaffNm = Staff.Range("Q" & LevelNumb + 2).Value
            Supervisor = Staff.Range("R" & LevelNumb + 2).Value
            Position = Staff.Range("S" & LevelNumb + 2).Value
            PicName = Staff.Range("V" & LevelNumb + 2).Value
            PicFile = PicFolder & "\" & PicName 'Full Picture File
            If Dir(PicFile, vbDirectory) = "" Then
                PicFile = DefaultPic
                If Dir(PicFile, vbDirectory) = "" Then PicFile = ""
            End If
            If LevelNumb <> 1 And Staff.Range("R" & LevelNumb + 2).Value <> Staff.Range("R" & LevelNumb + 1).Value Then 'Supervisor Change
                LeftPos = LeftPos + 20
            End If
              
               'Locate Supervisor Of staff

            Set FoundStaffRng = Staff.Range("Staff_Name").Find(Supervisor, , xlValues, xlWhole)
                .Shapes("SampleGrp" & Style).Duplicate.Name = "ChartItem" & StaffID
            With .Shapes("ChartItem" & StaffID)
                .Top = TopPos
                .Left = LeftPos
                .GroupItems("StaffDesc").TextFrame2.TextRange.Text = StaffNm & vbCrLf & Position
                If PicFile <> "" Then .GroupItems("StaffPic").Fill.UserPicture PicFile 'Set STaff Picture
                LeftPos = LeftPos + ShapeSpacer
               
            End With
           
            If Not FoundStaffRng Is Nothing Then 'Found Supevrvisor
                SupID = Staff.Range("A" & FoundStaffRng.Row).Value 'Supervisor ID
                'Add Connector
                .Shapes("SampleConn" & Style).Duplicate.Name = "ChartItem" & SupID & "_" & StaffID
                'Connect Profile shapes
                With .Shapes("ChartItem" & SupID & "_" & StaffID).ConnectorFormat
                    .BeginConnect OrgChart.Shapes("ChartItem" & SupID).GroupItems("StaffDesc"), 3
                    .EndConnect OrgChart.Shapes("ChartItem" & StaffID).GroupItems("StaffPic"), 1
                End With
               
        End If
        Set FoundStaffRng = Nothing
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.

Forum statistics

Threads
1,223,896
Messages
6,175,262
Members
452,627
Latest member
KitkatToby

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top