If selection is made from listbox remove same item from another listbox and comboboxes

Pookiemeister

Well-known Member
Joined
Jan 6, 2012
Messages
626
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
I have two listboxes and 32 comboboxes each containing the same information and that is a list of peoples names. The subroutine "toPopulate" populates the listboxes and comboboxes. The first listbox "listbox1" is for PTO and the second is for vacation. This is for a schedule maker and based on who is scheduled off their name will be removed from the second listbox "listbox2" and all 32 comboboxes, combobox1--->combobox32. The code below this one works but when it loops again, it does not remove the second and other selections that are made. Thank you.

VBA Code:
Sub toPopulate(n As Long)
    
    Dim i As Long, l As Long
    Dim tx As String
    Dim X
    
    d.RemoveAll
    e.RemoveAll

    For i = 1 To 32
        tx = UserForm1.Controls("ComboBox" & i).Text
        If tx <> "" And i <> n Then e(tx) = Empty
    Next
    
    If e.Count <> 0 Then
        For Each X In va
            If Not e.Exists(X) Then d(X) = Empty
        Next
    Else
        For Each X In va
            d(X) = Empty
        Next
    End If
    
    UserForm1.Controls("ComboBox" & n).List = d.keys
    UserForm1.Controls("ListBox" & n).List = d.keys

End Sub

VBA Code:
Private Sub ListBox1_Change()
    Dim index As Integer
    Dim cell As Range

    For index = 0 To UserForm1.ListBox1.ListCount - 1
        If UserForm1.ListBox1.Selected(index) = True Then
            MsgBox Me.ListBox1.List(index)
        End If
        'Ensure ListBox contains list items
        If ListBox1.ListCount >= 1 Then
        'If no selection, choose last list item.
            If ListBox1.ListIndex = -1 Then
                ListBox1.ListIndex = ListBox1.ListCount - 1
            End If
       
    End If
    Next
    ListBox2.RemoveItem (ListBox1.ListIndex)
    ComboBox1.RemoveItem (ListBox1.ListIndex)
End Sub
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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