Userform with Multiple ListBoxes not working

E

ExcelMonkey

I have a userform which has two listboxes. Listbox1 lists
all these sheets in my spreadsheet. there is an Add and
Delete button which allows teh user to click on items in
Listbox1 and add them to Listbox2. When I click onto 1
item in Listbox1 and then press Add, the code fails. I
can't figure out why this is failing. It was working
before. I am getting a:

Run Time Error
Type mismatch error

The last line of code that says:

ListBox2.AddItem ListBox1.Value

Has a null value for ListBox1.Value. This usually has the
name of the sheet I have selected. Another thing is that
I cannot remember what cbDuplicates is. Its not a
variable name.

Private Sub AddButton_Click()
If ListBox1.ListIndex = -1 Then Exit Sub
If Not cbDuplicates Then
' See if item already exists
For i = 0 To ListBox2.ListCount - 1
If ListBox1.Value = ListBox2.List(i) Then
Beep
Exit Sub
End If
Next i
End If
ListBox2.AddItem ListBox1.Value
End Sub
 
B

Bob Phillips

I can't get the error myself, it works fine.

Maybe cbDuplicates is some Boolean variable set when the listbox item is
selected?

--

HTH

RP
(remove nothere from the email address if mailing direct)
 
E

ExcelMonkey

Its funny because I copies this from an old macro I had
without much thought as to how it worked. It was working
fine at first and then it stopped. I just did a search in
my code on cbDuplicates and it only comes up with 1
occurence - the one in the code snippet I posted. What
role is that playing in that specific line of code? I
can't seem to figure out why its there and what it is
doing?
 
E

ExcelMonkey

Its funny because I copies this from an old macro I had
without much thought as to how it worked. It was working
fine at first and then it stopped. I just did a search in
my code on cbDuplicates and it only comes up with 1
occurence - the one in the code snippet I posted. What
role is that playing in that specific line of code? I
can't seem to figure out why its there and what it is
doing?
 
E

ExcelMonkey

Hey it works when I change the multiselect property back
to fm MultiselectSingle. I had it on fm
MultiselectMulti. Is the cbDuplicates affecting that
property? What changes would I make if I in fact want the
property set to fm MultiselectMulti.

Thanks
 
B

Bob Phillips

It won't have any effect if it is not being set anywhere else, as the
variable will be empty, so it will pass that test. I commented it out, no
difference.

--

HTH

RP
(remove nothere from the email address if mailing direct)
 
D

Doug Glancy

You'll use the Select property of each listbox1 item. In XL03 the help
example for the Select property does almost exactly what you want. Here's
the critical bit:

For i = 0 To 9
If ListBox1.Selected(i) = True Then
ListBox2.AddItem ListBox1.List(i)
End If
Next i

hth,

Doug Glancy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top