Consulting

Results 1 to 4 of 4

Thread: Get Unique list of cells in a Column for loading a Combobox

  1. #1
    VBAX Regular
    Joined
    Sep 2018
    Posts
    14
    Location

    Lightbulb Get Unique list of cells in a Column for loading a Combobox

    Hi,

    I have following data in a column (B):

    C02/2401
    C02/2801
    C02/2401
    C02/2801
    C03/1015
    C03/1006
    C03/1015
    C03/1006
    C03/1006
    C03/1015
    C03/1015
    C03/1006
    I am wondering if it is possible to remove duplicates in the list and load unique and sorted values to a Combobox placed on a VBA form.

    thanks

  2. #2
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    Option Explicit
    
    Sub test()
        Dim c As Range
        Dim s As String
        
        With CreateObject("system.collections.arraylist")
            For Each c In Sheets("sheet1").Columns("B").SpecialCells(xlCellTypeConstants)
                s = c.Value
                If Not .contains(s) Then .Add s
            Next
            .Sort
            ComboBox1.List = .toarray
        End With
    
    End Sub

  3. #3
    VBAX Regular
    Joined
    Sep 2018
    Posts
    14
    Location
    Quote Originally Posted by mana View Post
    Option Explicit
    
    Sub test()
        Dim c As Range
        Dim s As String
        
        With CreateObject("system.collections.arraylist")
            For Each c In Sheets("sheet1").Columns("B").SpecialCells(xlCellTypeConstants)
                s = c.Value
                If Not .contains(s) Then .Add s
            Next
            .Sort
            ComboBox1.List = .toarray
        End With
    
    End Sub

    Hello Mana,
    Many thanks for your prompt response, I am using following code, wondering if you can advise on that..


    Dim myJointCodeRange As Range
    Set myJointCodeRange = Application.Range("B:B")

    Dim myRow As Range
    Dim myVal As Variant

    For Each myRow In myJointCodeRange.Cells

    myVal = myRow.Value

    If Not InStr(myVal, "/") = 0 Then
    Me.cmbJointID.AddItem (myVal)
    End If

    Next

  4. #4
    VBAX Regular
    Joined
    Sep 2018
    Posts
    14
    Location
    Hello everyone,
    will be thankful for any feedback; here is my existing code..


    Dim myJointCodeRange As Range
    Set myJointCodeRange = Application.Range("B:B")

    Dim myRow As Range
    Dim myVal As Variant

    For Each myRow In myJointCodeRange.Cells

    myVal = myRow.Value

    If Not InStr(myVal, "/") = 0 Then
    Me.cmbJointID.AddItem (myVal)
    End If

    Next

Posting Permissions

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