【问题标题】:How to delete an element from an Array in userform VBA如何在用户窗体 VBA 中从数组中删除元素
【发布时间】:2021-10-16 20:13:45
【问题描述】:

我有一个包含以下代码的用户表单。基本上它的作用是如果用户选择一个标题,然后单击 commandButton2 将所选电影标题插入到标题框和数组中。现在我制作了另一个按钮commandButton3,用户可以从标题框中选择要删除的标题,但我正在努力从我正在构建的数组中删除它。提前致谢。

Public SelectedTitles As Variant, ArrCount As Long, EnableEvents As Boolean
Private Sub CommandButton1_Click()
'Done Button
    Me.Hide
End Sub
Private Sub CommandButton2_Click()
    'User has indicated they want to add the currently selected item to the list
    If Not EnableEvents Then Exit Sub
    If ArrCount = 0 Then 'First item, create the array
        SelectedTitles = Array("")
    Else
        ReDim Preserve SelectedTitles(UBound(SelectedTitles) + 1) 'Next items, add one more space in the array
    End If
    
    'Add the selected title to the array
    SelectedTitles(ArrCount) = ComboBox1.Value
    ListBox1.AddItem (SelectedTitles(ArrCount))
    'Increment the counter
    ArrCount = ArrCount + 1
    'Reset the checkbox and the combobox
    EnableEvents = False
    CommandButton2.Value = False
    ComboBox1.Value = ""
    EnableEvents = True
End Sub
Private Sub CommandButton3_Click()
    For i = ListBox1.ListCount - 1 To 0 Step -1
        If ListBox1.Selected(i) Then
            ListBox1.RemoveItem i
                If SelectedTitles() = i Then
                    SelectedTitles() = "n/a"
                End If
             ArrCount = ArrCount - 1
        End If
     Next i
     
    EnableEvents = False
    CommandButton3.Value = False
    ComboBox1.Value = ""
    EnableEvents = True
End Sub
Private Sub CommandButton4_Click()
    ListBox1.Clear
    Erase SelectedTitles
    ArrCount = 0
    EnableEvents = False
    CommandButton4.Value = False
    ComboBox1.Value = ""
    EnableEvents = True
End Sub
Private Sub ListBox1_Click()
End Sub
Private Sub UserForm_Initialize()
    EnableEvents = True
End Sub

【问题讨论】:

  • 您需要将数组复制到没有选择元素的新数组中。最好看看使用集合。 docs.microsoft.com/en-us/office/vba/language/reference/…
  • 你不能从列表框中填充数组吗?
  • @Norie 我什么时候会这样做。如果我要这样做,那么我想我必须将我拥有的命令按钮 2 的代码放在命令按钮 1 中,这将基于列表框创建数组?

标签: arrays excel vba userform


【解决方案1】:

首先,在 VBA 中使用数组可能会很痛苦。您可以考虑改用 ArrayLists,这样使用起来要方便得多。检查这个:https://excelmacromastery.com/vba-arraylist/

假设您的数组是一个数组列表,您可以这样做:

For i = ListBox1.ListCount - 1 To 0 Step -1
    If ListBox1.Selected(i) Then
        ListBox1.RemoveItem i
        SelectedTitles.RemoveAt i
    End If
 Next i

但如果你想坚持你的阵列,你必须做这样的事情:

For i = ListBox1.ListCount - 1 To 0 Step -1
    If ListBox1.Selected(i) Then
        ListBox1.RemoveItem i
           For j = 0 to Ubound(SelectedTitles)-1
              If SelectedTitles(j) <> SelectedTitles (i) then
                 ReDim Preserve TempSelectedTitles(UBound(TempSelectedTitles) + 1)
                 TempSelectedTitles(j) =  SelectedTitles(i)
              end if
           next  J
           SelectedTitles = TempSelectedTitle
    End If
  Next i

我没有尝试这段代码,但你明白了,你需要重建你的数组,而不在你的列表框中包含选定的值

【讨论】:

  • 嗨@Josselin Jankowski 感谢您的帮助!可悲的是,我认为我没有使用 arraylist,但是使用您发送的第二个代码得到了错误“Invalid ReDim”,为什么会这样。我是否必须初始化 TempSelctedTitles。谢谢
  • 但是听起来,如果我可以将上面 CommandButton2 中的 SelectedTitles 数组更改为 ArrayList,我的问题就会得到解决
  • @JosselinJankowski 认为如果你在循环外执行它,你可以避免重复 ReDim Preserve
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-20
  • 2023-02-17
  • 1970-01-01
相关资源
最近更新 更多