Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88066

How to remove duplicate values from combobox | excel vba |

$
0
0

I have a scenario where i want to populate unique values into combobox excluding duplicate values

My sheet details

Sheet

Code :

Private Sub ComboBoxscname_DropButtonClick()
    With Worksheets("A1")
                ComboBoxscname.List = .Range("B2:B"& .Cells(.Rows.Count, "A").End(xlUp).Row).Value
    End With
End Sub

I have highlighted with yellow which are duplicated for column "B" it should be displayed only once in combobox

Another solution i have but getting error when selecting specific sheet name

Sub ComboBoxscnameList()

Dim LR As Long
Dim ctrl As Object
'Set ctrl = Sheets("A1").Select

LR = Cells(Rows.Count, "B").End(xlUp).Row

ctrl.List() = CreateArray(Range("B2:B"& LR))

End Sub

'creates an array from a given range
'ignores blanks and duplicates

Function CreateArray(r As Range)
    Dim col As New Collection, c As Range, TempArray(), i As Long

    'for each cell in range r
    For Each c In r
        On Error Resume Next
        col.Add c.Value, CStr(c.Value)
        If Err.Number = 0 And Trim(c) <> "" Then
            ReDim Preserve TempArray(i)
            TempArray(i) = c.Value
            i = i + 1
        End If
        Err.Clear
    Next

    CreateArray = TempArray
    Erase TempArray

End Function

Private Sub ComboBoxscname_DropButtonClick()
Call ComboBoxscnameList            
End Sub

Viewing all articles
Browse latest Browse all 88066

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>