Also filter column in listbox in another column

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
6,170
Office Version
  1. 2024
Platform
  1. Windows
Code in use shown below

The listbox is populated & when the use makes a selection its filtered for column 4th column
Can an edit be made so its also filtered on 2nd column.

Maybe & if possible when a user clicks in the listbox a option will be shown for 2nd / 4th column then proceed to filer as requested

Rich (BB code):
Private Sub ListBox1_Click()
    Dim i As Long
    Dim colour As String
    
    ' filter listbox on 4th column
    colour = ListBox1.List(ListBox1.ListIndex, 3)
    For i = ListBox1.ListCount - 1 To 0 Step -1
        If ListBox1.List(i, 3) <> colour Then
            ListBox1.RemoveItem (i)
        End If
    Next i
           
    ' how many of this color
     Me.TextBox2 = ListBox1.ListCount & "  " & colour
End Sub
 
Post #8 worked fine.
I was asking before i tried it as i was out at the time.

Just a question.
If a user doesnt select a option button & clicks in the listbox they will see this RTE,debug to line shown in red.
Is there something we can do about that or do we just run with it.

Thanks


Rich (BB code):
Private Sub ListBox1_Click()
    Dim i As Long, filtStr As String, listCol As Long
    Select Case True
        Case OptionButton1.Value
            filtStr = ListBox1.List(ListBox1.ListIndex, 3)
            listCol = 3
        Case OptionButton2.Value
            filtStr = ListBox1.List(ListBox1.ListIndex, 1)
            listCol = 1
    End Select

    For i = ListBox1.ListCount - 1 To 0 Step -1
        If ListBox1.List(i, listCol) <> filtStr Then
            ListBox1.RemoveItem (i)
        End If
    Next i

    ' how many of filter results
     Me.TextBox2 = ListBox1.ListCount
End Sub
 

Attachments

  • EaseUS_2025_04_ 9_09_13_16.jpg
    EaseUS_2025_04_ 9_09_13_16.jpg
    10.7 KB · Views: 4
Upvote 0
Just a question.
If a user doesnt select a option button & clicks in the listbox they will see this RTE,debug to line shown in red.
Is there something we can do about that or do we just run with it.
Re-read the suggestion,
Rich (BB code):
You could use 2 option buttons on the form, initializing the most commonly used one to be true in UserForm_Initialize
and that situation will never arise
 
Upvote 0

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