我的用户窗体上有两个列表框。第一个列表框链接到一个表。目标是双击某个项目后,该值将插入到第二个列表框中。
我尝试防止重复项出现在第二个列表框中,这样您就不会意外地将同一项目插入两次。
Private Sub listbox_variante_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Dim x As Integer
For x = 0 To listbox_variante.ListCount - 1
If listbox_variante.Selected(x) = True Then
listbox_seleccion.AddItem (listbox_variante.List(x))
End If
Next x
End Sub
Private Sub UserForm_Initialize()
listbox_variante.RowSource = "Tabla2[DESCRIPCIÓN]"
End Sub
如何防止从
Listbox_variante
到 Listbox_seleccion
添加重复的项目?
我还没有测试过,但尝试一下...
Private Sub listbox_variante_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Dim x As Long
Dim y As Long
Dim bFound As Boolean
bFound = False
For x = 0 To listbox_variante.ListCount - 1
If listbox_variante.Selected(x) = True Then
For y = 0 To listbox_seleccion.ListCount - 1
If listbox_seleccion.List(y) = listbox_variante.List(x) Then
Beep 'optional
bFound = True
Exit For
End If
Next y
If Not bFound Then
listbox_seleccion.AddItem listbox_variante.List(x)
Else
bFound = False
End If
End If
Next x
End Sub