Run code if lost box contains selected item

Nitefox

New Member
Joined
Sep 23, 2013
Messages
22
I have two list boxes on a userform, on which only one may contain a selected item at any one time. I am trying to code a click box that says:
When click box is unchecked then if listbox1 contains a selected item then run this code, if listbox2 contains a selected item then run this code.
I can't figure out the coding for the checkbox when its unselected, and also the coding to check if a list box contains a selected item.
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Are you wanting to run the code if the Item in the list box that is selected is a specific value or just if the list box has a Item selected?
 
Upvote 0
For the checkbox use the Click or Change event and check what the value of the checkbook is to see if it's checked or unchecked.

For the listboxes, assuming they are single select, check their ListIndex property, if it's -1 nothing has been selected.
 
Upvote 0
I have the following code running when the checkbox gets checked, Im looking go add more code to run when the checkbox gets unchecked.
Code:
Private Sub CheckBox3_Click()
Dim A As Long
Dim B As Long
Dim Match As Boolean
Dim Unit As String


    If Me.CheckBox3 Then
        'Get Unit listbox selected record column 1
        For A = 0 To Me.lbUnitList.ListCount
            If Me.lbUnitList.Selected(A) Then
                Unit = Me.lbUnitList.List(A, 0)
            End If
        Next


        A = 0


        Do
            Match = False
            'Is lbActiveItemList column 3, Receive or Relocate
            Select Case Me.lbActiveItemList.List(A, 2)
            Case "Receive", "Relocate"
                If Me.lbActiveItemList.List(A, 5) = Unit Then
                    Do
                        For B = 1 To Me.lbActiveItemList.ListCount - 1
                            If lbActiveItemList.List(A, 4) = lbActiveItemList.List(B, 4) Then
                                Select Case lbActiveItemList.List(B, 2)
                                Case "Return", "Lost", "Relocate"
                                    Me.lbActiveItemList.RemoveItem B
                                    Me.lbActiveItemList.RemoveItem A
                                    Match = True
                                    A = 0
                                    Debug.Print Me.lbActiveItemList.ListCount
                                    Exit Do


                                End Select


                            End If
                        Next
                        A = A + 1
                    Loop Until B = Me.lbActiveItemList.ListCount
                End If
            End Select
        Loop While Match
        
        'Purge remaining records
        For A = Me.lbActiveItemList.ListCount - 1 To 0 Step -1
            If Me.lbActiveItemList.List(A, 5) = Unit Then
                Select Case Me.lbActiveItemList.List(A, 2)


                Case "Return", "Damaged", "Lost"
                    Me.lbActiveItemList.RemoveItem (A)
                End Select
            End If
        Next


        For A = Me.lbActiveItemList.ListCount - 1 To 0 Step -1
            If Me.lbActiveItemList.List(A, 7) = Unit Then
                Select Case Me.lbActiveItemList.List(A, 2)


                Case "Relocate"
                    Me.lbActiveItemList.RemoveItem (A)
                End Select
            End If
        Next
    End If
End Sub
The list boxes are single select only. Would this code work (it should run the code if a selection is present):

<code style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif;">If ListBox1.ListIndex > -1 then
</code>...
If
ListBox2.ListIndex > -1 then
...
 
Upvote 0
All you should need to do is add an Else to the initial If to handle the checkbox being unchecked.
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,257
Members
449,075
Latest member
staticfluids

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