Duplicate First Value Loading into ComboBOx

BrianExcel

Well-known Member
Joined
Apr 21, 2010
Messages
975
I am using teh following code to identify a range, filter out all duplicate values and then populate a ComboBox based on that range.

Code:
Sub Userform_Initialize()Dim wbBook As Workbook
Dim wsSheet As Worksheet
Dim rnData As Range


'Variant to contain the data to be placed in the combo box.
Dim vaData As Variant


'Initialize the Excel objects
Set wbBook = ThisWorkbook
Set wsSheet = wbBook.Worksheets("Expense Account Guide")


'Set the range equal to the data, and then (temporarily) copy the unique values of that data to the L column.
With wsSheet
    Set rnData = .Range(.Range("A1"), .Range("A500").End(xlUp))
        rnData.AdvancedFilter Action:=xlFilterCopy, CopyToRange:=.Range("L1"), Unique:=True
    
    'store the unique values in vaData
    vaData = .Range(.Range("L1"), .Range("L500").End(xlUp)).Value
    
    'clean up the contents of the temporary data storage
    Range(.Range("L1"), .Range("L50").End(xlUp)).ClearContents
End With


'display the unique values in vaData in the combo box already in existence on the worksheet.
With frmExpenseAccountGuide.cmbAccountType
    .Clear
    .List = vaData
End With


End Sub

For some reason, with this code, it copies the first value twice. When I am done, the list successfully loads with the following values, in order:

Corporate Use
Corporate Use
Marketing
PM IC

I dont know why it's copying that first "corporate use" twice. There is more than one occurrence of that value, but I have checked and all are identical (no extra spaces, incorrect spellings, etc.).

Thoughts?
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
I found this code, which works to remove the duplicate value, but this adds an empty space at the end of the combobox:

Code:
Dim v, eWith Sheets("Expense Account Guide").Range("A1:A500")
    v = .Value
End With
With CreateObject("scripting.dictionary")
    .comparemode = 1
    For Each e In v
        If Not .exists(e) Then .Add e, Nothing
    Next
    If .Count Then frmExpenseAccountGuide.cmbAccountType.List = Application.Transpose(.keys)
End With

Getting closer...
 
Upvote 0
If I change the start of the range to A2 instead of the current A1, it removes the header from the ComboBox, but it still has a blank on the end (with the new code)...
 
Upvote 0

Forum statistics

Threads
1,215,360
Messages
6,124,493
Members
449,166
Latest member
hokjock

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