H
Harry-Wishes
Hello, I am trying to fill a Listbox of a userform with unique values from an
existing array. The array contains multiple instances of the same values. I
took a snippet of code below from an excel forum which essentially does the
trick except it pulls unique values from a range of cells of an Excel
worksheet. Of course, it works well in Excel as you'd expect but I can't seem
to adapt this example when I try to pull unique values from an array. If you
look at the subroutine immediately below, you see that it calls a function
which accepts a range of cells as a parameter. I'm trying to adapt the
subroutine and function so that I can pass in an array rather than a range of
cells. VB does not like what I'm doing so far as I'm not well versed in
passing in parameters. I can't imagine there would be much to change but I
could be wrong. Any help is welcome.
Thanks
In case you're wondering, I am working in Word, not Excel.
Private Sub UserForm_Initialize()
Dim MyUniqueList As Variant, i As Long
With Me.ListBox1
.Clear ' clear the listbox content
MyUniqueList = UniqueItemList(Range("A4:A100"), True)
For i = 1 To UBound(MyUniqueList)
.AddItem MyUniqueList(i)
Next i
.ListIndex = 0 ' select the first item
End With
End Sub
Private Function UniqueItemList(InputRange As Range, _
HorizontalList As Boolean) As Variant
Dim cl As Range, cUnique As New Collection, i As Long, uList() As Variant
Application.Volatile
On Error Resume Next
For Each cl In InputRange
If cl.Formula <> "" Then
cUnique.Add cl.Value, CStr(cl.Value)
End If
Next cl
UniqueItemList = ""
If cUnique.Count > 0 Then
ReDim uList(1 To cUnique.Count)
For i = 1 To cUnique.Count
uList(i) = cUnique(i)
Next i
UniqueItemList = uList
If Not HorizontalList Then
UniqueItemList = _
Application.WorksheetFunction.Transpose(UniqueItemList)
End If
End If
On Error GoTo 0
End Function
existing array. The array contains multiple instances of the same values. I
took a snippet of code below from an excel forum which essentially does the
trick except it pulls unique values from a range of cells of an Excel
worksheet. Of course, it works well in Excel as you'd expect but I can't seem
to adapt this example when I try to pull unique values from an array. If you
look at the subroutine immediately below, you see that it calls a function
which accepts a range of cells as a parameter. I'm trying to adapt the
subroutine and function so that I can pass in an array rather than a range of
cells. VB does not like what I'm doing so far as I'm not well versed in
passing in parameters. I can't imagine there would be much to change but I
could be wrong. Any help is welcome.
Thanks
In case you're wondering, I am working in Word, not Excel.
Private Sub UserForm_Initialize()
Dim MyUniqueList As Variant, i As Long
With Me.ListBox1
.Clear ' clear the listbox content
MyUniqueList = UniqueItemList(Range("A4:A100"), True)
For i = 1 To UBound(MyUniqueList)
.AddItem MyUniqueList(i)
Next i
.ListIndex = 0 ' select the first item
End With
End Sub
Private Function UniqueItemList(InputRange As Range, _
HorizontalList As Boolean) As Variant
Dim cl As Range, cUnique As New Collection, i As Long, uList() As Variant
Application.Volatile
On Error Resume Next
For Each cl In InputRange
If cl.Formula <> "" Then
cUnique.Add cl.Value, CStr(cl.Value)
End If
Next cl
UniqueItemList = ""
If cUnique.Count > 0 Then
ReDim uList(1 To cUnique.Count)
For i = 1 To cUnique.Count
uList(i) = cUnique(i)
Next i
UniqueItemList = uList
If Not HorizontalList Then
UniqueItemList = _
Application.WorksheetFunction.Transpose(UniqueItemList)
End If
End If
On Error GoTo 0
End Function