ListBox 1 + SpinButton + Listbox 2 - how to populate?

Cascade

New Member
Joined
Aug 1, 2010
Messages
9
Hi there,

I have a list box 1 and a spin button, and a list box 2.

When the user selects an item in list box 1, and presses the spin button the item will go to listbox 2.

Anyone have any advice on how to do this programattically?
Or a link they could refer me to?

chooseproductslistboxsp.png

http://imageshack.us/photo/my-images/844/chooseproductslistboxsp.png/

Image is attached.

Thanks,
Rgds.
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Rather than a spin button, I would use two command buttons.
This code is for a spin button and a multi-select ListBox1 and mono-select ListBox2.

Code:
Private Sub SpinButton1_SpinDown()
    Rem code for single select listbox
    With ListBox2
        If .ListIndex <> -1 Then
            ListBox1.AddItem .List(.ListIndex)
            .RemoveItem .ListIndex
            .ListIndex = -1
        End If
    End With
End Sub

Private Sub SpinButton1_SpinUp()
    Rem code for multiselect listbox
    Dim i As Long
    With ListBox1
        For i = .ListCount - 1 To 0 Step -1
            If .Selected(i) Then
                ListBox2.AddItem .List(i)
                .RemoveItem i
            End If
        Next i
    End With
End Sub
 
Upvote 0
Okay so I ended up using two command buttons:
- Add >>>
- Remove

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

End Sub

Private Sub RemoveButton_Click()
    If lbProductsChosen.ListIndex = -1 Then Exit Sub
    lbProductsChosen.RemoveItem lbProductsChosen.ListIndex
End Sub

The code works.

Although there are a couple things I don't understand:
1. why is it necessary to state:
Code:
 If lbProducts.ListIndex = -1 Then Exit Sub

2. I can get a multiselect listbox feature going. It can only select one at a time.
When I change the properties of the listbox to "2- fmMultiSelectExtended" and run the code, I get an error.
The debugger highlights the last line of the code as the problem in yellow:

Code:
   lbProductsChosen.RemoveItem lbProductsChosen.ListIndex

Anyone, thanks.
chooseproductslistboxco.png

Image Link: http://img88.imageshack.us/img88/7076/chooseproductslistboxco.png
 
Upvote 0

Forum statistics

Threads
1,214,923
Messages
6,122,286
Members
449,076
Latest member
kenyanscott

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top