Results 1 to 7 of 7

Thread: adding distinct value in combobox...

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,942

    adding distinct value in combobox...

    Have a userforn in a vba for excel project
    In column A (started from A2) have alist of value:
    AAAAA
    AAAAA
    BBBBBB
    CCCCC
    CCCCC
    CCCCC
    ....

    how to fill combobox without dupes from column A?

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: adding distinct value in combobox...

    The answer lies in your thread which I answered in March 6th 2009

    http://www.vbforums.com/showthread.php?t=560486
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: adding distinct value in combobox...

    If your worksheet has 5000 items in column A, you should NOT add all of them to the combobox then search to remove 4900 duplicate items, ie. Do not add an item if it is already there.

    This is one way to do it: (items added to combobox will also be sorted)
    Code:
    Private Sub UserForm_Initialize()
        Dim sItem As String
        Dim i As Long
        Dim r As Long
        
        '-- use CStr() to prevent cell contains error:
        '   #N/A    will be converted to "Error 2042",
        '   #DIV/0! will be converted to "Error 2007", ...
        With Me.ComboBox1
            r = 2
            sItem = CStr(Sheet1.Cells(r, 1))
            Do While Len(sItem) > 0
                For i = 0 To .ListCount - 1
                    If sItem <= .List(i) Then Exit For
                Next
                If i >= .ListCount Then
                    .AddItem sItem
                ElseIf sItem < .List(i) Then
                    .AddItem sItem
                '-- if sItem = .List(i) then do not add it
                End If
                r = r + 1
                sItem = CStr(Sheet1.Cells(r, 1))
            Loop
        End With
    End Sub
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  4. #4
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: adding distinct value in combobox...

    Luca what is your data range? Remember time taken to sort data (In your Case) is totally dependent on the number of items that you want to sort and not on the time taken to add items to combobox...

    Adding of items whether it is 5 items, 1000 items, 10000 items or 655356 items in your case will take less than 1 second!!!
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  5. #5
    Banned
    Join Date
    Aug 2009
    Posts
    14

    Re: adding distinct value in combobox...

    Quote Originally Posted by koolsid View Post
    Luca what is your data range? Remember time taken to sort data (In your Case) is totally dependent on the number of items that you want to sort and not on the time taken to add items to combobox...

    Adding of items whether it is 5 items, 1000 items, 10000 items or 655356 items in your case will take less than 1 second!!!
    This is a stup.. advice that I have ever seen!
    The time to sort on adding is much less than the time to search to remove duplicate items.
    Wake up! The way the Oz guy uses is the best choice.

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,942

    Re: adding distinct value in combobox...

    Quote Originally Posted by koolsid View Post
    The answer lies in your thread which I answered in March 6th 2009

    http://www.vbforums.com/showthread.php?t=560486
    Wow!!!!!!!!!!!!
    tks to remember me this post!
    note:
    But wth you are in your mind? A Gigabit of hardisk and a 3 Giga of memeory?

  7. #7
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: adding distinct value in combobox...

    Quote Originally Posted by luca90 View Post
    Wow!!!!!!!!!!!!
    tks to remember me this post!
    note:
    But wth you are in your mind? A Gigabit of hardisk and a 3 Giga of memeory?
    Glad to be of help
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

Posting Permissions

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



Click Here to Expand Forum to Full Width