【发布时间】:2015-01-23 11:34:21
【问题描述】:
我正在构建一个包含两个 ListBox 的用户窗体,以便用户可以从 ListBox1 中选择选项并将它们添加到 ListBox2 或从 ListBox2 中删除选项
我正在努力解决的是如何防止将重复项添加到 ListBox2 中?本质上,我想构建一个函数(?)来检查一个选项是否已经包含在 ListBox2 中
Private Sub CommandButton3_Click()
'### Adds Items from ListBox1 to ListBox2
For i = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(i) = True Then ListBox2.AddItem ListBox1.List(i)
Next i
ListBox1.Selected
End Sub
Private Sub CommandButton4_Click()
'### Removes Items from ListBox2
Dim counter As Integer
counter = 0
For i = 0 To ListBox2.ListCount - 1
If ListBox2.Selected(i - counter) Then
ListBox2.RemoveItem (i - counter)
counter = counter + 1
End If
Next i
End Sub
【问题讨论】:
-
使用包含函数添加检查
-
请发布您的图片链接...
-
不确定包含函数如何在 VBA 中工作。但是,我在 stackoverflow 上发现了另一个类似的线程,它解决了类似的问题并努力根据我的需要进行调整:stackoverflow.com/questions/19755920/…