Stopping duplications been entered into a listbox
I was confused about the slist and tlist, but this seemed to work ok for me.
I did add a label to indicate any errors:
Option Explicit
Private Sub UserForm_Initialize()
With Me.ListBox1
.MultiSelect = fmMultiSelectMulti
.AddItem "Jan-Mar"
.AddItem "Apr-Jun"
.AddItem "Jul-Sep"
.AddItem "Oct-Dec"
.AddItem "Quarterly Calendar Year"
End With
With Me.ListBox2
.MultiSelect = fmMultiSelectSingle
.AddItem "2000"
.AddItem "2001"
.AddItem "2002"
.AddItem "2003"
.AddItem "2004"
End With
Me.Label1.Caption = ""
End Sub
Private Sub CommandButton1_Click()
Dim tlist As String
Dim lIndex As Long
Dim OkToAdd As Boolean
Dim iCtr As Long
Dim LB1HasASelection As Boolean
If Me.ListBox2.ListIndex < 0 Then
Beep 'nothing selected inme.listbox2
Me.Label1.Caption = "Please select a year!"
Exit Sub
End If
LB1HasASelection = False
For lIndex = 0 To Me.ListBox1.ListCount - 1
If Me.ListBox1.Selected(lIndex) = True Then
LB1HasASelection = True
tlist = Me.ListBox1.List(lIndex) & " " & Me.ListBox2.Value
OkToAdd = True
For iCtr = 0 To Me.ListBox3.ListCount - 1
If tlist = Me.ListBox3.List(iCtr) Then
OkToAdd = False
Exit For
End If
Next iCtr
If OkToAdd Then
Me.ListBox3.AddItem tlist
End If
End If
Next lIndex
If LB1HasASelection = False Then
Me.Label1.Caption = "Please select at least one calendar option"
Beep
Else
Me.Label1.Caption = ""
End If
End Sub
bluewatermist wrote:
Hi
I'm hoping you can assist as i can't find any relevant information on my
problem. I have two listboxes on a userform. When a command button is
clicked, it combines listbox1 and listbox2 together and creates a period into
listbox3. Listbox1 & listbox2 information are created when the userform is
Initialised. listbox1 is a multi select and listbox2 is a single select.
Example below:
Private Sub UserForm_Initialize()
With Me.ListBox1
.AddItem "Jan-Mar"
.AddItem "Apr-Jun"
.AddItem "Jul-Sep"
.AddItem "Oct-Dec"
.AddItem "Quarterly Calendar Year"
end With
With Me.ListBox2
.AddItem "2000"
.AddItem "2001"
.AddItem "2002"
.AddItem "2003"
.AddItem "2004"
end With
end Sub
Private Sub CommandButton1_Click()
Dim slist As String
Dim tlist As String
Dim lIndex As Long
For lIndex = 0 To 22
If ListBox1.Selected(lIndex) Then
slist = slist & ", " & ListBox1.List(lIndex)
tlist = " " & ListBox2.Value
End If
Next
ListBox3.AddItem Mid(slist, 3) & tlist
end Sub
My query is how can i stop duplications from been entered into listbox3?
--
Dave Peterson
|