Count items in listbox

Bandito1

Board Regular
Joined
Oct 18, 2018
Messages
235
Office Version
  1. 2016
Platform
  1. Windows
Hi all,

I have a listbox named lstSearchResults that displays search results.

I would like to display in a label the amount of results that are found.

I thought i use this code but it doesn't work;

Code:
Me.Label2.Caption = lstSearchResults.ListCount

It displays the wrong number and doesn't change if i search on other terms.

Someome knows the code to display the number of search results?
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
I woud expect this to work if they are both on a worksheet or both on a userform (and the code in the appropriate module)
Code:
Private Sub lstSearchResults_Change()
    Label2.Caption = lstSearchResults.ListCount
End Sub

1. Would be helpful to see the whole code (including Sub..End Sub - how do we know what triggers the event otherwise?)

2. Are they on a userform or a worksheet

3. Where is the code?

4. Why is one of the objects prefixed by Me in your code ( if one does not require it, neither does the other or prefix both - consistent but does the same thing! )

5. How are the search results added to lstSearchResults ?
- please post the code that puts the results in the listbox
 
Upvote 0
Hi, thanks for your reply

This is the full code;

Code:
Option Explicit
Dim Ary As Variant

Private Sub lstSearchResults_MouseMove( _
           ByVal Button As Integer, ByVal Shift As Integer, _
           ByVal X As Single, ByVal Y As Single)
    HookListBoxScroll
End Sub


Private Sub lstSearchResults_Change()
    Label2.Caption = lstSearchResults.ListCount
End Sub


Private Sub txtKeywords_Change()
   Dim r As Long, c As Long, nr As Long
   Dim X As String
   ReDim Nary(1 To UBound(Ary), 1 To 8) As Variant
   
   lstSearchResults.Clear
   txtAreaSearch.Text = ""
   txtFunctionSearch.Text = ""
   txtShiftSearch.Text = ""
   For r = 1 To UBound(Ary)
      X = Join(Application.Index(Ary, r, 0), "")
      If InStr(1, X, Me.txtKeywords, vbTextCompare) Then
         nr = nr + 1
         For c = 1 To UBound(Ary, 2)
            Nary(nr, c) = Ary(r, c)
         Next c
      End If
   Next r
   lstSearchResults.List = Nary
End Sub


Private Sub UserForm_Initialize()
   Ary = Sheet3.Range("A2", Sheet3.Range("A" & Rows.Count).End(xlUp).Offset(, 7)).Value
   With Me.lstSearchResults
      .ColumnCount = 8
      .ColumnWidths = "150;70;48;70;70;50;50"
      .List = Ary
   End With
End Sub

It's on a userform.

As you can see i added your suggetion but it doesn't work :(
 
Last edited:
Upvote 0
My logic
Nary is dimensioned the same as Ary (from which only selected items populate Nary) - therefore cannot use UBound(Nary)
Nary is populated from 1 to the last value of nr
nr is available only in txtKeywords_Change (where declared)

so try
- deleting procedure lstSearchResults_Change
- and inserting the red line below into txtKeywords_Change ...

Code:
Private Sub txtKeywords_Change()
...other code 
   lstSearchResults.List = Nary
   [COLOR=#ff0000]Label2.Caption = nr[/COLOR]
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,576
Messages
6,125,633
Members
449,242
Latest member
Mari_mariou

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