Need help loading Combo box and List box without Empty data or duplicates

jdr360

New Member
Joined
Nov 12, 2017
Messages
18
I just can't get anything to work, I have the following code which works great, but I have been trying to modify it to add more functionality to it. Like sorting, and duplicate removal. I think I got this code from this site, it's been a while since I had to work on this part of my userform I really do not remember.

Code:
Sub FillCombobox(WSName As String, ColLtr As String, CBox As ComboBox)


Dim LastRow As Long
Dim aCell As Range


Set ws = ActiveWorkbook.Worksheets(WSName)
    With ws
        LastRow = .Cells(.Rows.Count, ColLtr).End(xlUp).Row
        For Each aCell In .Range(ColLtr & "2:" & ColLtr & LastRow)
            If Not aCell.Value = "" Then
                CBox.AddItem aCell.Value
            End If
        Next
    End With
    
End Sub

It's a very simple and easy way to populate a combo box without empties, I really like this kinda of reusable code programming, its very smart and simple. I would like to add the ability to remove duplicates and possibly a sort feature. I have searched everywhere and unable to get anything to work. Like I said, the above code works as is removing blanks, but I need something more. I have tried the Dictionary list way and an array, but just not having luck getting anything working. The above code is in the module1 and works great. I call it with

Code:
Call FillCombobox("Bill To", "D", Me.cmbBillTo)

I am still learning VBA as I go. Thank you
James
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Try something like this for data in sheet "Mysheet", column "A" to fill "ComboBox1".
Code:
[COLOR="Navy"]Sub[/COLOR] MG08Dec30
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, AL [COLOR="Navy"]As[/COLOR] Object
[COLOR="Navy"]With[/COLOR] Sheets("MySheet")
        [COLOR="Navy"]Set[/COLOR] Rng = .Range(.Range("A2"), .Range("A" & Rows.Count).End(xlUp))
[COLOR="Navy"]End[/COLOR] With
    [COLOR="Navy"]Set[/COLOR] AL = CreateObject("System.Collections.ArrayList")
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
      [COLOR="Navy"]If[/COLOR] Not IsEmpty(Dn.Value) [COLOR="Navy"]Then[/COLOR]
          [COLOR="Navy"]If[/COLOR] Not AL.Contains(Dn.Value) [COLOR="Navy"]Then[/COLOR] AL.Add Dn.Value
       [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] Dn
    
    AL.Sort
[COLOR="Navy"]With[/COLOR] ComboBox1
    .Clear
    .List = AL.ToArray
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]With[/COLOR]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0

Forum statistics

Threads
1,223,901
Messages
6,175,277
Members
452,629
Latest member
SahilPolekar

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