Count Filtered Listbox

ExcelEndeavor

New Member
Joined
Oct 13, 2020
Messages
29
Office Version
  1. 365
Platform
  1. MacOS
I have a listbox ("Listbox1") that filters dynamically (no command button) and I want to display the count of results in a textbox ("Textbox1").

I have tried these two options at the end of my Filter_Data() sub:

VBA Code:
Textbox1 = Listbox1.ListCount

and

Code:
Me.Textbox1 = Me.Listbox.ListCount

but both options show all of the unfiltered results. How do I get it to only count the filtered results?
 
at the end of my Filter_Data() sub

If you are referring to this Sub, then the number of filtered items is at the value of 'k', add the line highlighted in blue.


Rich (BB code):
Sub Filter_Data()
  Dim i As Long, j As Long, k As Long
  Dim tbox As String, cbox As String, cad As String
  Dim col As Long, n As Long

  'Show no results in lbxResults unless ComboLookup item is selected
  If Me.TxtKeywords.Text = "" Then
    Me.lbxResults.Clear
    Exit Sub
  End If
 
  On Error Resume Next
    n = UBound(a, 1)
    If n = 0 Then
      MsgBox "no data"
      Exit Sub
    End If
  On Error GoTo 0
 
  ReDim b(1 To UBound(a, 1), 1 To UBound(a, 2))
  For i = 1 To UBound(a, 1)
    If col = 0 Then
      cad = "|" & LCase(a(i, 1)) & "|" & LCase(a(i, 2)) & "|" & LCase(a(i, 3)) & "|" & LCase(a(i, 4)) & "|" & LCase(a(i, 5)) & "|"
    Else
      cad = LCase(a(i, col))
    End If
   
    If TxtKeywords.Value = "" Then tbox = cad Else tbox = LCase(TxtKeywords.Value)
   
    If cad Like "*" & tbox & "*" Then
      k = k + 1
      For j = 1 To UBound(a, 2)
        b(k, j) = a(i, j)
      Next
    End If
  Next
 
  Me.lbxResults.List = b
  Textbox1 = k
End Sub


:giggle:
 
Upvote 0
Solution
If you are referring to this Sub, then the number of filtered items is at the value of 'k', add the line highlighted in blue.


Rich (BB code):
Sub Filter_Data()
  Dim i As Long, j As Long, k As Long
  Dim tbox As String, cbox As String, cad As String
  Dim col As Long, n As Long

  'Show no results in lbxResults unless ComboLookup item is selected
  If Me.TxtKeywords.Text = "" Then
    Me.lbxResults.Clear
    Exit Sub
  End If
 
  On Error Resume Next
    n = UBound(a, 1)
    If n = 0 Then
      MsgBox "no data"
      Exit Sub
    End If
  On Error GoTo 0
 
  ReDim b(1 To UBound(a, 1), 1 To UBound(a, 2))
  For i = 1 To UBound(a, 1)
    If col = 0 Then
      cad = "|" & LCase(a(i, 1)) & "|" & LCase(a(i, 2)) & "|" & LCase(a(i, 3)) & "|" & LCase(a(i, 4)) & "|" & LCase(a(i, 5)) & "|"
    Else
      cad = LCase(a(i, col))
    End If
  
    If TxtKeywords.Value = "" Then tbox = cad Else tbox = LCase(TxtKeywords.Value)
  
    If cad Like "*" & tbox & "*" Then
      k = k + 1
      For j = 1 To UBound(a, 2)
        b(k, j) = a(i, j)
      Next
    End If
  Next
 
  Me.lbxResults.List = b
  Textbox1 = k
End Sub


:giggle:
Hello again DanteAmor - It is actually a different sub that I created in a different project. I took your code from the last one you helped me with so it is similar. This helps me greatly - thank you again :)
 
Upvote 0
ExcelEndeavor,

Please Note: In the future, when marking a post as the solution, please mark the post that contains the solution (not your own post acknowledging that some other post was the solution).
When a post is marked as the solution, it is then shown right underneath the original question so people viewing the question can easily see the question and solution in a single quick glance without having to hunt through all the posts.

I have updated this thread for you.
 
Upvote 0

Forum statistics

Threads
1,226,870
Messages
6,193,439
Members
453,799
Latest member
shanley ducker

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