gpt4 book ai didi

excel - 从列表框中删除了错误的项目

转载 作者:行者123 更新时间:2023-12-02 18:02:59 28 4
gpt4 key购买 nike

在选择项目并单击相应的按钮后,项目将从 ListBox1 传输到 12 个列表框 (2-13) 中的任何一个。列表框 (2-13) 不能包含超过 8 个项目。

问题:如果从 Listbox1 的中间选择项目,则代码会将最后选定的项目保留在 Listbox1 中,并错误地从 Listbox1 中删除未选择的顶部项目。我不知道为什么。请查看屏幕截图和下面的代码:

ListBox1 中选定的项目:

enter image description here

按下“向左箭头”按钮后的结果:(testtwo.A.2F 项目已消失,testseven.A.2R 项目仍保留在 ListBox1 中。

enter image description here

“左”和“右”按钮的代码:

Private Sub BTN_MoveSelectedRight_Click()

Dim iCtr As Long

For iCtr = 0 To Me.ListBox2.ListCount - 1
If Me.ListBox2.Selected(iCtr) = True Then
Me.ListBox1.AddItem Me.ListBox2.List(iCtr)
End If
Next iCtr

For iCtr = Me.ListBox2.ListCount - 1 To 0 Step -1
If Me.ListBox2.Selected(iCtr) = True Then
Me.ListBox2.RemoveItem iCtr
End If
Next iCtr



End Sub
Private Sub BTN_MoveSelectedLeft_Click()

Dim iCtr As Long
Dim i As Long
Dim j As Long
Dim arr(8) As Long

For iCtr = 0 To Me.ListBox1.ListCount - 1

If Me.ListBox1.Selected(iCtr) = True And Not ListBox2.ListCount = 8 Then
Me.ListBox2.AddItem Me.ListBox1.List(iCtr)
i = i + 1
arr(i) = iCtr
End If

If i = 8 Then Exit For
Next iCtr

For j = i - 1 To 0 Step -1
Me.ListBox1.RemoveItem arr(j)
Next

End Sub

列表框 1 是 fmMultiSelectExtended,列表框 2-13 是 fmMultiSelectMulti。

谢谢。

最佳答案

我认为问题出在这一行:

   For j = i - 1 To 0 Step -1

它看起来不应该递减,因为它访问的是 arr 而不是列表。尝试:

   For j = i To 1 Step -1

关于excel - 从列表框中删除了错误的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34568450/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com