I'm using a function from this question, however, it doesn't seem to work in my case.
Basically, this script is going through a column selecting distinct values and populating array arr with them. First If is checking if the column has ended, then to avoid calling empty array I have the first IfElse, and finally I want to check a non-empty array for cell string. If it is not present, I want to add it.
Public Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
IsInArray = (UBound(Filter(arr, stringToBeFound)) > -1)
End Function
Sub SelectDistinct()
Dim arr() As String
Dim i As Integer
Dim cells As Range
Set cells = Worksheets("types").Columns("A").Cells
i = 0
For Each cell In cells
If IsEmpty(cell) Then
Exit For
ElseIf i = 0 Then
ReDim Preserve arr(i)
arr(UBound(arr)) = cell
i = i + 1
ElseIf IsInArray(cell.Value, arr) = False Then
ReDim Preserve arr(i)
arr(UBound(arr)) = cell
i = i + 1
End If
Next cell
End Sub
For some reason, it throws "Subscript out of range" error on the call of IsInArray function. Can someone let me know where I went wrong?
If IsError(Application.Match(cell.Value, arr, False))to check if the value already exists in the array.