+ Reply to Thread
Results 1 to 2 of 2

VBA combo box add unique values only

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    04-03-2009
    Location
    USA, California
    MS-Off Ver
    Excel 2007
    Posts
    385

    VBA combo box add unique values only

    I am trying to populate a combo box with unique values, what am I doing wrong? Getting runtime error '381': Could not set the List property. Invalid property array index
    errors out on this line: If LCase(frmMain.cmbCategory.List(r2)) = LCase(val) Then Exit Sub on the second pass, works during the first pass through though.
    LR = Sheets(3).Range("C" & Rows.Count).End(xlUp).Row
    
    Sheets(3).Select
    For r = r To LR Step 1
        val = Sheets(3).Range("C" & r2 + 2).value
        If LCase(frmMain.cmbCategory.List(r2)) = LCase(val) Then Exit Sub
        v = Sheets(3).Range("C" & r).value
        frmMain.cmbCategory.AddItem v, r2
        r2 = r2 + 1
    Next r

  2. #2
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,138

    Re: VBA combo box add unique values only

    This is how I do that.
    Private Sub UserForm_Initialize()
    
    Dim Unique As New Collection
    Dim StartRow As Long
    Dim LastRow As Long
    
    StartRow = 3
    
    With Sheets(3)
        LastRow = .Range("C" & .Rows.Count).End(xlUp).Row
        
        For A = StartRow To LastRow
            On Error Resume Next
            Unique.Add CStr(.Range("C" & A)), CStr(.Range("C" & A))
            On Error GoTo 0
        Next
    End With
    
        For A = 1 To Unique.Count
            frmMain.cmbCategory.AddItem Unique(A)
        Next
        
    End Sub
    A collection can't have the same values (Well keys actually, but in this case they are the same), so we take advantage of that by trying to add the whole range. But the On Error skips the duplicate entries so that you end up with a unique list.

    NitPiks
    1. avoid using Val as a variable name since it is also a function A = Val("1") would return the NUMBER 1.
    2. Since you are referring to a specific range, there's no need to select the sheet first. (Unless you have a specific need to.)
    3. If you count positively through a loop, Step 1 is the default, so there's no need to spell it out.
    David
    (*) Reputation points appreciated.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] cascading combo boxes and unique values
    By union in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-03-2013, 05:38 PM
  2. Multiple dependant filtered with unique values combo box in userform
    By dorman.castillo in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-29-2013, 04:43 PM
  3. scaling an exisiting macro for unique values of X for Unique values of Y.
    By widodude in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-18-2011, 09:33 PM
  4. Combo Box list of unique values in range
    By Jason_2112 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 11-10-2010, 02:10 PM
  5. Populate combo box with unique values only
    By sjayar in forum Excel General
    Replies: 1
    Last Post: 11-07-2005, 03:35 AM

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