View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.programming
bluewatermist bluewatermist is offline
external usenet poster
 
Posts: 26
Default Stopping duplications been entered into a listbox


Hi Dave

if you add Jan-Mar, Apr-Jun, Jul-Sep 2002 then if for example selected
Apr-Jun 2002 again that would be considered a dupe. Also if you selected
again Jan-Mar, Apr-Jun, Jul-Sep 2002 they would be dupes.

I know what you're saying but i just wanted the year to be at the end of the
periods selected. Currrently listbox1 has if statements to stop from
selecting all periods and quarterly calendar year at the same time.

Do you think I'm asking too much?

"Dave Peterson" wrote:

I wondered why you did it that way.

If you add: Jan-Mar, Apr-Jun, Jul-Sep 2002
then what is considered a duplicate:

Does it have to be:
Jan-Mar, Apr-Jun, Jul-Sep 2002
or
are
Jan-Mar 2002
Apr-Jun 2002
Jul-Sep 2002
all duplicates

Would Quarterly Calendar Year make any other choice a duplicate?

This kind of thing would make the code more challenging. I think I would bite
the bullet and create one listbox with all the options for each year and not try
to combine two listboxes this way.

.AddItem "Jan-Mar 2002"
.AddItem "Apr-Jun 2002"
.AddItem "Jul-Sep 2002"
.AddItem "Oct-Dec 2002"
.AddItem "Quarterly Calendar Year 2002"
.AddItem "Jan-Mar 2003"
.AddItem "Apr-Jun 2003"
.AddItem "Jul-Sep 2003"
.AddItem "Oct-Dec 2003"
.AddItem "Quarterly Calendar Year 2004"
.AddItem "Jan-Mar 2004"
.AddItem "Apr-Jun 2004"
.AddItem "Jul-Sep 2004"
.AddItem "Oct-Dec 2004"
.AddItem "Quarterly Calendar Year 2004"


And those quarter calendar year options will even make this more difficult--you
shouldn't be able to seelect all 4 quarters and the quarterly calendar year for
the same year, right?



bluewatermist wrote:

Hi Dave

I tried your coding and it works but like my original one if you select for
example Jan-Mar and Apr-Jun and Jul-Sep and then select 2002 it would place
this in listbox3 as Jan-Mar, Apr-Jun, Jul-Sep 2002. I have tried to change
your code but without success. Are you able to give me a suggestion.

Many thanks.

"Dave Peterson" wrote:

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


--

Dave Peterson