我有两个列表框。ListBox1 具有项目列表,用户可以通过双击项目或按下添加按钮来选择这些项目以传输到 ListBox2。我现在要做的是防止用户在 ListBox2 中添加重复项。如果检测到重复项,则会提示“项目已包含”并结束代码。我猜这可以用包含来完成?但我不知道该怎么做。我有以下代码:
'Report Listing
Private Sub UserForm_Initialize()
'List of Reports
With ListBox1
.AddItem "Report 1"
.AddItem "Report 2"
.AddItem "Report 3"
.AddItem "Report 4"
.AddItem "Report 5"
.AddItem "Report 6"
End With
End Sub
'Add selection to ListBox2
Private Sub AddButton_Click()
With ListBox1
Dim itemIndex As Integer
For itemIndex = .ListCount - 1 To 0 Step -1
If .Selected(itemIndex) Then
ListBox2.AddItem .List(itemIndex)
End If
Next itemIndex
End With
End Sub
'Double click to Add
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
ListBox2.AddItem ListBox1.List(ListBox1.ListIndex)
End Sub