CompressPageRanges

Function CompressPageRanges(ByVal pageList As String) As String
Dim pages() As String
Dim i As Long
Dim result As String
Dim startPage As Long
Dim endPage As Long

pages = Split(pageList, ",")

For i = 0 To UBound(pages)
If startPage = 0 Then
startPage = CLng(pages(i))
endPage = startPage
ElseIf CLng(pages(i)) = endPage + 1 Then
endPage = CLng(pages(i))
Else
If result <> "" Then result = result & ","
If startPage = endPage Then
result = result & CStr(startPage)
Else
result = result & CStr(startPage) & "-" & CStr(endPage)
End If
startPage = CLng(pages(i))
endPage = startPage
End If
Next i

If result <> "" Then result = result & ","
If startPage = endPage Then
result = result & CStr(startPage)
Else
result = result & CStr(startPage) & "-" & CStr(endPage)
End If

CompressPageRanges = result
End Function

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *