We help IT Professionals succeed at work.

Fill listbox with unique values from first column in flexgrid

Wilder1626
Wilder1626 asked
on
601 Views
Last Modified: 2012-06-27
Hello all,

I'm trying to feed my list1 with unique value from column 1 of my MSHflexgrid2 but nothing update with this list.

Would you know why?

I'm trying to see but i'm getting out of idea.

Thanks again for your help



 'Fill listbox with unique values from first column in flexgrid
 Dim xlObject     As Excel.Application
Dim xlWB         As Excel.Workbook
Dim NoOfRows     As Long
Dim NoOfColumns  As Long
     
     Dim vArray As Variant
    Dim lngRow As Long
    Dim lngCol As Long
    Dim dicUnique As New Scripting.Dictionary
    Dim vItem As Variant
    
    Me.List1.Clear
    For lngRow = 1 To NoOfRows
        vItem = MSHFlexGrid2.TextMatrix(lngRow, 1)
        If dicUnique.Exists(vItem) Then
        Else
            dicUnique.Add vItem, 1
            List1.AddItem vItem
        End If
    Next
    dicUnique.RemoveAll
    Set dicUnique = Nothing

Open in new window

TEST2.xlsx
Comment
Watch Question

Martin LissSocial distance - Don't touch your face - Wash your hands for 20 seconds
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
There's at least one problem and that is that NoOfRows is 0 so the code in the loop is never executed.
Social distance - Don't touch your face - Wash your hands for 20 seconds
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Top Expert 2009

Commented:
MartinLiss is right. Right after "Me.List1.Clear" and before your "For lngRow = 1 To NoOfRows" statement, use:

NoOfRows = MSHFlexGrid2.Rows -1
Martin LissSocial distance - Don't touch your face - Wash your hands for 20 seconds
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
And VBClassic guy is right it should be -1 but you really don't need NoOfRows.
CERTIFIED EXPERT

Author

Commented:
Thanks again,

It is working great now

    Me.List1.Clear
    For lngRow = 1 To MSHFlexGrid2.Rows - 1
        vItem = MSHFlexGrid2.TextMatrix(lngRow, 1)
        If dicUnique.Exists(vItem) Then
        Else
            dicUnique.Add vItem, 1
            List1.AddItem vItem
        End If
    Next
    dicUnique.RemoveAll
    Set dicUnique = Nothing
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.