+ Reply to Thread
Results 1 to 6 of 6

Remove duplicates and sort data for populating a combobox using VBA

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-23-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    211

    Remove duplicates and sort data for populating a combobox using VBA

    Hi all

    I am trying to populate a combobox on a userform with data list from a sheet (strSheetName). I want to first remove duplicates and blanks from the list, sort the data in decending order and then populate a combobox.

    I have the code to remove duplicates and populate the combobox as shown below, but I don't know how to amend it to remove blanks and to sort it. Can anyone help advise?

    Private Sub Populate_cboSupplier()
        Dim Lr As Long, cell As Range
        'Populate Combobox1 with unique names from Sheets(1) column D
        With Sheets(strSheetName)
            Lr = .Range("H" & Rows.Count).End(xlUp).Row
            .Range("H2:H" & Lr).AdvancedFilter Action:=xlFilterInPlace, Unique:=True
            cboSuppliers.Clear
            For Each cell In .Range("H3:H" & Lr).SpecialCells(xlCellTypeVisible)
                cboSuppliers.AddItem cell.Value
            Next cell
            On Error Resume Next
            .ShowAllData
            On Error GoTo 0
        End With
    End Sub

  2. #2
    Forum Contributor
    Join Date
    07-23-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    211

    Re: Remove duplicates and sort data for populating a combobox using VBA

    Any suggestions?

  3. #3
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Remove duplicates and sort data for populating a combobox using VBA

    Other method
    Private Sub Populate_cboSupplier()
        Dim a, e
        cboSuppliers.Clear
        With Sheets(strSheetName)
            a = .Range("H2", .Range("H" & Rows.Count).End(xlUp)).Value
        End With
        With CreateObject("System.Collections.ArrayList")
            For Each e In a
                If (e <> "") * (Not .Contains(CStr(e))) Then
                    .Add CStr(e)
                End If
            Next
            .Sort: .Reverse
            cboSuppliers.List = .ToArray
        End With
    End Sub

  4. #4
    Forum Contributor
    Join Date
    07-23-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    211

    Re: Remove duplicates and sort data for populating a combobox using VBA

    Jindon

    You have come to my rescue again! Works much better now, but it's giving list from z to a. How can I make it a to z?

    Thanks very much

  5. #5
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Remove duplicates and sort data for populating a combobox using VBA

    A-Z...
    Remove
    : .Reverse

  6. #6
    Forum Contributor
    Join Date
    07-23-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    211

    Re: Remove duplicates and sort data for populating a combobox using VBA

    Sorted! (pardon the pun!)

    Thanks, friend

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1