Count the amount of entries in a List Box

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,226
Office Version
  1. 2007
Platform
  1. Windows
Morning,
I am using the code below & also attach a screenshot of the userform.
As you can see the Listbox shows all the codes taken from column J when the userform is open.

What i would like is to know / happen is how many entries are in the ListBox & then put that figure in the TextBox1
So i would open the userform, the listbox entries are loded, TextBox1 would show example 59


I did try this but TextBox was still blanl.
Rich (BB code):
Private Sub TextBox1_Click()
ListBox1.ListCount
End Sub


Rich (BB code):
Private Sub UserForm_Initialize()
    With ThisWorkbook.Worksheets("DATABASE")
        Dim data As Variant
        data = .Range("J6:K" & .Cells(.Rows.Count, "J").End(xlUp).Row).Value
    End With
 
    ReDim arr(0 To 1, 0 To UBound(data) - 1) As String
 
    Dim itm As String
    Dim cnt As Long
    Dim i As Long
 
    cnt = 0
    For i = LBound(data) To UBound(data)
        itm = data(i, 1)
        If itm Like "[A-Za-z]###" Then
            arr(0, cnt) = itm
            arr(1, cnt) = data(i, 2)
            cnt = cnt + 1
        End If
    Next i
 
    If cnt > 1 Then
        ReDim Preserve arr(0 To 1, 0 To cnt - 1)
        Sort2DArray arr()
        Me.ListBox1.List = Application.Transpose(arr())
    ElseIf cnt > 0 Then
        Me.ListBox1.Column = arr()
    End If

End Sub
 

Attachments

  • 4639.jpg
    4639.jpg
    221.3 KB · Views: 13

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
The code you showed
VBA Code:
Private Sub TextBox1_Click()
ListBox1.ListCount
End Sub
runs when the textbox is clicked. It sounds like you want to instead set this number as soon as the listbox is populated. Also, this line of code is just the count of items. It doesn't assign it to anything. That is, this code does not do anything.

Delete that sub, and add the following line at the end of UserForm_Initialize:

VBA Code:
TextBox1 = ListBox1.ListCount
 
Upvote 0
@6StringJazzer many thanks,i did spot the mistake as youve mentioned so i changed it to change event.

Now i have put the advised code in the correct place as mentioned & works fine.

Just on a side note say currently there are 100 entries BUT two are the same thus duplicates.
How would i tackle not to count the dupluicate ?
So the TRUE count is 99 as opposed to 100
This would also need to be applied for future entries.

Thanks
 
Upvote 0

Forum statistics

Threads
1,215,046
Messages
6,122,855
Members
449,096
Latest member
Erald

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