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.
 

Some videos you may like

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.

Scott M

Board Regular
Joined
Dec 31, 2011
Messages
152
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?
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,218
Office Version
  1. 365
Platform
  1. Windows
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.
 

Nitefox

New Member
Joined
Sep 23, 2013
Messages
22
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
...
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,218
Office Version
  1. 365
Platform
  1. Windows
All you should need to do is add an Else to the initial If to handle the checkbox being unchecked.
 

Watch MrExcel Video

Forum statistics

Threads
1,122,658
Messages
5,597,403
Members
414,142
Latest member
Banyangt

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
Top