Solved

get count of items in a listbox from another listbox on userform

Posted on 2014-04-02
7
243 Views
Last Modified: 2014-04-07
userform
excel 2010 vba
2 listboxes
Listbox1
Listbox2

First:
I'm populating  listbox1  first from Listbox2(specifically from column 38)

So listbox1 may contain :
Pumps
Motors
Hammers

What I need:
I need to add a count , the the items in listbox1 from Listbox2(column 38)

Example: I had 17 items in Listbox2.  

Listbox1 would now look like.
Pumps (8)
Motors (5)
Hammers (4)

Thanks
fordraiders

' populating listbox1 from Listbox2
ri = 0
UserForm2.ListBox1.Clear
With UserForm2.ListBox2
    For ri = .ListCount - 1 To 0 Step -1
        UserForm2.ListBox1.AddItem .List(ri, 38)
    Next
End With

Open in new window

0
Comment
Question by:fordraiders
  • 4
  • 2
7 Comments
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 39974130
How about a working file?
0
 
LVL 48

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 39974201
Hi,

pls try ( With reference to Microsoft Scripting Runtime)

Dim Array1  As Variant
Dim Dict As Dictionary

Array1 = Application.Index(ListBox1.List, , 38)
Set Dict = New Dictionary
For Idx = LBound(Array1, 1) To UBound(Array1, 1)
    If Dict.Exists(Array1(Idx, 1)) Then
        Dict.Item(Array1(Idx, 1)) = Dict.Item(Array1(Idx, 1)) + 1
    Else
        Dict.Add Array1(Idx, 1), 1
    End If
Next
For Each strKey In Dict.Keys()
    Me.ListBox2.AddItem (strKey & " (" & Dict(strKey) & ")")
Next

Open in new window

Regards
0
 
LVL 3

Author Closing Comment

by:fordraiders
ID: 39975806
Thanks
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 3

Author Comment

by:fordraiders
ID: 39979736
would this work also ?
Dim kk As Variant
kk = UserForm2.ListBox32.List

For ri = 0 To UserForm2.ListBox49.ListCount - 1
 
   With UserForm2.ListBox32
    For jj = .ListCount - 1 To 0 Step -1
       For Each kk In UserForm2.ListBox32
        If UserForm2.ListBox32.List(jj, 38) = UserForm2.ListBox49.List(ri, 0) Then
        UserForm2.ListBox49.List(ri, 0) = UserForm2.ListBox49.List(ri, 0)
        cc = cc + 1
        End If
        Next kk
    Next jj
    UserForm2.ListBox49.List(ri, 1) = "(" & cc & ")"  
    End With
    cc = 0
Next ri
0
 
LVL 3

Author Comment

by:fordraiders
ID: 39979765
Array1 = Application.Index(ListBox1.List, , 38)

  THIS ARRAY stays empty ?

routine not working properly...sorry...
0
 
LVL 48

Expert Comment

by:Rgonzo1971
ID: 39982685
HI,

Have you tried to use

Array1 = Application.Index(UserForm2.ListBox32.List, , 38)
0
 
LVL 3

Author Comment

by:fordraiders
ID: 39984851
yes. That's.
Array1 = Application.Index(UserForm2.ListBox32.List, , 38)  
that's what I used.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Recurring Excel Timelime for Veeam 2 32
TT Column Arrange 10 27
+1 to an even value 4 11
Import data into excel from web page 10 7
A2 = A1 That kind of cell reference is relative.  If you copy it from A2 to B2, then B2 will get this: B2 = B1 That's all fine and good, but if you then insert a new row above row 2, you'll find: A3 = A1 B3 = B1 This is intentional. …
INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now