Trouble populating multi column ActiveX ListBox with unique values from 2 dimensional range (VBA)

L

Legacy 332279

Guest
Hey,

I'm trying to populate a ActiveX ListBox through VBA with unique values from a two dimensional range, but I can't seem to find a way to both only get unique, sorted values and populate two columns with those values. That is to say, I have no issues with populating a single column in the listbox with unique, sorted values or populating two columns without omitting duplicate values, but I don't get how to do both.

So, in essence, what i would like to accomplish is to populate the ListBox with unique, sorted values from a two dimensional range (for example "A1:B10") into two columns. To clarify, the sum of both columns in each row should be unique so that, as an example, both row 1 and 2 are added to the listbox, but not row 3, based on the example range below:

AB
AC
AC

<tbody>
</tbody>

Currently I'm working on the basis of the following script, which populates the listbox with a single column of unique, sorted values:

Code:
Sub Update_ListBox1()

L_ID = Sheets("ID-Data").Range("a1:a100")

With CreateObject("System.Collections.ArrayList")[INDENT]For Each cl In L_ID[/INDENT]
[INDENT=2]If cl <> "" And Not .contains(cl) Then .Add cl[/INDENT]
[INDENT]Next[/INDENT]
[INDENT].Sort[/INDENT]
[INDENT]ListBox1.List = Application.Transpose(.toarray())[/INDENT]
End With


End Sub

Any ideas on how I could add B1:B100 to the second column of the listbox while still keeping the values unique and sorted as described above?
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Just unique...hope this helps

Code:
Sub Update_ListBox1()
 Dim Dic, i As Long, buf As String, Keys, x()
    Set Dic = CreateObject("Scripting.Dictionary")
    On Error Resume Next
    For i = 2 To 10
        buf = Cells(i, 1).Value & "," & Cells(i, 2).Value
        Dic.Add buf, buf
    Next i
    
    Keys = Dic.Keys
        
        For i = 0 To Dic.Count - 1
        ReDim Preserve x(i)
           x(i) = Replace(Keys(i), ",", "")
         Next i
        ListBox1.List = x
    Set Dic = Nothing
End Sub
 
Upvote 0
Thanks, but unfortunaley I do need the values to be populated across two columns in the listbox in order to get the selected item's values from the columns seperately. I think I can get the values sorted another way, so if the list turns out sorted or not isn't that important at this stage, but getting the unique values populated across two columns is.
 
Upvote 0

Forum statistics

Threads
1,216,099
Messages
6,128,820
Members
449,469
Latest member
Kingwi11y

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