How to remove a Label that was dynamically created in VBA?

2

The code below, sent by Luiz Vieira, creates Labels dynamically and a click-handling function for each Label.

I would like to know how to use the Click event to remove the Labels that were created dynamically.

Creating Labels:

Dim Labels() As New LabelHandler

Private Sub CriaLabels(ByVal QuantidadeDeLabels As Integer)

    Dim i As Integer
    Dim Label As Control

    ReDim Labels(0 To QuantidadeDeLabels - 1)

    For i = 0 To QuantidadeDeLabels - 1

        Set Label = Me.Controls.Add("Forms.Label.1", "NewLabel" & i)

        With Label
          .Caption = "NewLabel" & i
          .Top = 50 * i
          .Left = 50
        End With

        Set Labels(i).Ctrl = Label

    Next i

End Sub

Click treatment function in the class module:

Public WithEvents Ctrl As MSForms.Label

Private Sub Ctrl_Click()

    MsgBox "Você clicou no label de nome " & Ctrl.Name

End Sub
    
asked by anonymous 21.06.2016 / 14:18

1 answer

0

Edelson, use the instruction below:

Me.Controls.Remove Labels(0).Name

Change the index to delete the Label you want, in this case, the index is zero.

I usually delete from the last index until the first, to avoid problems.

    
21.06.2016 / 23:47