Multiple Listboxes, how do I know which one I have selected?

dreid1011

Well-known Member
Joined
Jun 4, 2015
Messages
3,613
Office Version
  1. 365
Platform
  1. Windows
Good morning,

I have created a userform with 3 listboxes. Items are added to each listbox individually as the user inputs data into a corresponding text box. I am trying to add a delete item button, that when clicked, removes the selected item from whichever listbox the selected item is in. How do I tell Excel which listbox the item is in?

So far, Google searches yield results pertaining to multiple selections in a listbox, but not a single selection within multiple listboxes.

Any help is greatly appreciated, thank you.
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Try this:-
Code:
Private Sub CommandButton1_Click()
Dim aList As Variant, Lt As Variant, n As Long
aList = Array(ListBox1, ListBox2, ListBox3)
For Each Lt In aList
    With Lt
       For n = 0 To .ListCount - 1
            If .Selected(n) Then
                .RemoveItem (n)
            End If
        Next n
    End With
Lt.ListIndex = -1
Next Lt
End Sub
 
Upvote 0
Thank you, that code worked well. Here is what I have now after some modifications to accommodate the multipage on the form:

Code:
Dim ListArr As Variant, ListN As Variant
Dim n As Long
Dim CtrlName(4) As String
CtrlName(0) = "Counter"
CtrlName(1) = "Cashier1"
CtrlName(2) = "Cashier2"
CtrlName(3) = "Cashier3"
CtrlName(4) = "Cashier4"
ListArr = Array(MultiPage1.Pages(MultiPage1.Value).Controls(CtrlName(MultiPage1.Value) & "ChecksList"), MultiPage1.Pages(MultiPage1.Value).Controls(CtrlName(MultiPage1.Value) & "CChecksList"), MultiPage1.Pages(MultiPage1.Value).Controls(CtrlName(MultiPage1.Value) & "MOrdersList"))
For Each ListN In ListArr
    With ListN
        For n = 0 To .ListCount - 1
            If .Selected(n) Then
                .RemoveItem (n)
            End If
        Next n
    End With
ListN.ListIndex = -1
Next ListN

I'm going to clean it up a little more even later.
 
Upvote 0

Forum statistics

Threads
1,223,636
Messages
6,173,484
Members
452,516
Latest member
archcalx

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