Given an array of numbers:
[1,3,4,5,8,9,11]
What's the simplest way in VBA to convert that list to a readable string, e.g:
1, 3-5, 8-9, 11
I could just rewrite my VB.net function to VBA but it's already quite long winded and it will end up even longer in VBA.
Public Shared Function GroupedNumbers(nums As List(Of Long))
If nums Is Nothing OrElse nums.Count = 0 Then Return "-"
If nums.Count = 1 Then Return nums(0)
Dim lNums = nums.Distinct().OrderBy(Function(m) m).ToList
Dim curPos As Long = 1
Dim lastNum As Long = lNums(0)
Dim i As Long = 0
Dim numStr As String = lNums(0)
Dim isGap As Boolean = False
Do Until i >= lNums.Count - 1
Do Until i >= lNums.Count - 1 OrElse lNums(i) + 1 <> lNums(i + 1)
i += 1
isGap = True
Loop
If isGap Then
numStr += "-" & lNums(i)
End If
If i <> lNums.Count - 1 Then
numStr += ", " & lNums(i + 1)
isGap = False
i += 1
End If
Loop
Return numStr
End Function
Just wondering if anyone has a better way of doing this before i go about rewriting it for VBA?