Create A List From Listbox Selections but exclude any selections where 2nd column = 0

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,564
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have a multiselect listbox (uf1_listbox2) in my userform (uf1_assess_sched).

It has 2 columns, one holding text, the other a number.

I am trying to generate a list on a worksheet (to form a criteria range for an advanced filter) of the values selected from the listbox.

Code:
Private Sub cbtn_clean_Click()

    Dim temp_ws As Worksheet
    Dim afh_col As Integer, i As Integer, icount As Integer
    
    Set temp_ws = Workbooks("schedule.csv").Worksheets("temp_ws")
    
    'check if anything selected
    For i = 0 To uf1_listbox2.ListCount - 1
        If uf1_listbox2.Selected(i) = True Then
        icount = icount + 1
        End If
    Next i
    If icount = 0 Then
        MsgBox "Please make a selection before trying to clean.", vbInformation, "ERROR: No selections"
        Exit Sub
    End If
    
    'create advanced filter list
    afh_col = 1 'starting point of advanced filter criteria range
    For i = 0 To uf1_listbox2.ListCount - 1
        If uf1_listbox2.Selected(i) = True Then
            If uf1_listbox2.List(i) = "Reference" Then
                r_type = "ref"
            ElseIf uf1_listbox2.List(i) = "Function" Then
                r_type = "fcn"
            ElseIf uf1_listbox2.List(i) = "Facility" Then
                r_type = "fac"
            ElseIf uf1_listbox2.List(i) = "Unid'd Class" Then
                r_type = "X"
            Else
                r_type = "X2"
            End If
         'advanced filter criteria range
            ws_vh.Cells(10, afh_col).Value = r_type
            afh_col = afh_col + 1
        End If
    Next i
   
End Sub

This code seems to do the trick, however, I do not want to include any selection in the criteria range if the number value (2nd column) of the selection in the listbox is 0.

How can I exclude selected rows from the listbox whose second column value = 0? Or, prevent the user from selecting any rows from the listbox in the first place in which column 2 = 0?
 
Last edited:

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Something like this will keep those items from being selected

Code:
Sub ListBox1_Change()
    Static Abort As Boolean
    Dim i As Long

    If Abort Then Exit Sub

    Abort = True

    With ListBox1
        For i = 0 to .ListCount - 1
            If .List(i, 1) = "0" Then                
                .Selected(i) = False
            End If
        Next i
    End With

    Abort = False
End Sub
 
Upvote 0
Perfect Mike!!! Thanks so much. Works exactly as I was hoping.
 
Upvote 0

Forum statistics

Threads
1,214,652
Messages
6,120,747
Members
448,989
Latest member
mariah3

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