如何删除VBA中的最后一项

时间:2018-11-08 10:12:19

标签: excel vba userform

我有以下内容,每次单击userForm中的按钮时,都会添加1个editBox和1个comboBox:

Sub addCable()

    'TextBox creation
    Dim editTxtBox As MSForms.Control
    Static iTxtBox As Integer
    Set editTxtBox = Me.Controls.Add("Forms.TextBox.1")
    iTxtBox = iTxtBox + 1
    cbxs.Add editTxtBox
    ', editTxtBox.Name
    With editTxtBox
        .Name = "txtBox" & iTxtBox
        .Top = iTxtBox * editTxtBox.Height + 10
        .Left = 20
    End With

    'ComboBox creation
    Dim editCmpBox As MSForms.Control
    Static iCmpBox As Integer
    Set editCmpBox = Me.Controls.Add("Forms.ComboBox.1")
    iCmpBox = iCmpBox + 1
    cbxs.Add editCmpBox
    ', editCmpBox.Name
    With editCmpBox
        .Name = "cmBox" & iCmpBox
        .Top = iCmpBox * editCmpBox.Height + 10
        .Left = 130
        .List = Array("60", "125", "45")
    End With
End Sub

我想实现撤消功能,因为它删除了我单击按钮时创建的最后一个exitBox和最后一个comboBox。 我当时想遍历我的cbxs集合并从索引iTxtBox处删除,因为iTxtBox是全局变量:

Dim num As Integer
MsgBox (iTxtBox)
    For num = iTxtBox To cbxs.Count
        cbxs.Remove iTxtBox
    Next

但是它不起作用,我想这是因为我的循环超出了我想要的范围。...

0 个答案:

没有答案