title: Excel VBA - Dynamic Userform with Add/Remove Object Functionality
body: <p>I am trying to create a userform whereby people can place as many requests as they want with the ability to remove requests they no longer want, as well as other functionalities. I am having an issue with removing the dynamically created objects after the functions add-remove-add has been used in that sequence.</p>
  
  <p>The code I have below has a snippet of the objects added to the userform along with dimension changes to the userform and objects already embedded in the userform. Other defined portions are not included below.</p>
  
  <pre><code>Dim RemoveButtonArray() As New Class_RemoveRequest
  
  For i = Last To Last
      Set AddRemoveButton = GenPurchaseRequest.Controls.Add("Forms.Image.1", "btnRemove" &amp; ObjID)
      With AddRemoveButton
          'properties
      End With
      Set AddRemoveLabel = GenPurchaseRequest.Controls.Add("Forms.Label.1", "lblRemove" &amp; ObjID)
      With AddRemoveLabel
          'properties
      End With
      Set AddRequest = GenPurchaseRequest.Controls.Add("Forms.Frame.1", "Frame" &amp; ObjID)
      With AddRequest
          'properties
          .Caption = "Purchase Request - " &amp; ObjID
      End With
      With AddRequestButton
          .Top = 168 + (126 * i)
          .Left = 18
      End With
      With SubmitButton
          .Top = 168 + (126 * i)
          .Left = 200
      End With
      With CancelButton
          .Top = 168 + (126 * i)
          .Left = 381
      End With
      With GenPurchaseRequest
          .ScrollHeight = 200 + (126 * i)
          .ScrollTop = 200 + (126 * i)
      End With
  ReDim Preserve RemoveButtonArray(0 To i)
  Set RemoveButtonArray(i).RemoveButton = AddRemoveButton
  Next i
  
  ObjID = ObjID + 1
  Last = Last + 1
  </code></pre>
  
  <p>This works well and the form is populated with everything correctly. When the user removes a request, the below code works fine as well:</p>
  
  <pre><code>Public WithEvents RemoveButton As MSForms.Image
  
  Private Sub RemoveButton_click()
  
  Dim ConfirmRemoval As Integer
  Dim rbRefNo As String
  Dim rbRefNoConvert As Integer
  
  ConfirmRemoval = MsgBox("Are you sure you would like to remove this request?", vbYesNo)
  
  If ConfirmRemoval = vbYes Then
  rbRefNo = Mid(Me.RemoveButton.Name, 10)
  rbRefNoConvert = CInt(rbRefNo)
      With GenPurchaseRequest
          If Last &gt; 1 Then
          .Controls.Remove ("Frame" &amp; rbRefNo)
          .Controls.Remove ("btnRemove" &amp; rbRefNo)
          .Controls.Remove ("lblRemove" &amp; rbRefNo)
  
              For i = rbRefNoConvert + 1 To Last - 1
              .Controls("Frame" &amp; i).Top = .Controls("Frame" &amp; i).Top - 126
              .Controls("btnRemove" &amp; i).Top = .Controls("btnRemove" &amp; i).Top - 126
              .Controls("lblRemove" &amp; i).Top = .Controls("lblRemove" &amp; i).Top - 126
              Next i
  
          .AddRequestButton.Top = .AddRequestButton.Top - 126
          .SubmitButton.Top = .SubmitButton.Top - 126
          .CancelButton.Top = .CancelButton.Top - 126
          .ScrollTop = .ScrollTop - 126
          .ScrollHeight = .ScrollHeight - 126
  
          Last = Last - 1
  
           Else
           MsgBox "There is only one active Purchase Request."
           End If
       End With
  Else
  'do nothing
  End If
  
  End Sub
  </code></pre>
  
  <p>The user can then go back add additional requests as well as remove more requests that they no longer want. The problem arises when they add more requests and then attempt to remove the last one added directly after the removal. For example: I added 4 requests and then removed the 2nd one. I then added another request, but wanted to remove the 4th request, however, the remove button no longer works.</p>
  
  <p>I believe that the issue is that I need to redefine the array used to store the removal buttons once the remove button function is called, however I have no idea how to do that. My current attempt at doing that is:</p>
  
  <pre><code>For j = 0 To Last
  If j = rbRefNoConvert Then
  j = j + 1
  Else
  ReDim RemoveButtonArray(0 To j)
  Set RemoveButtonArray(j).RemoveButton = AddRemoveButton
  End If
  Next j
  </code></pre>
  
  <p>But that object reference is incorrect and I do not know how to reference it correctly. I tried referencing the control itself, but that did not work.</p>
  
  <p>I am very new to the use of class modules, arrays, and dynamic userforms, so sorry for the lengthy question!</p>
  
  <p>Any help would be very much appreciated!</p>