Solved

# How do I group a list of numbers into a condensed form...

Posted on 2004-11-10
217 Views
Hi,

I am trying to write a function to convert the following string...
1, 2, 3, 4, 8, 9, 14, 16, 17 ,18
into...
1-4, 8-9, 14, 16-18

Any help would be appreciated..

James
0
Question by:jatkin

LVL 5

Accepted Solution

Well, its not the neatest solution but I think it works OK :)

Public Sub test()

Dim sInput      As String
Dim sOutput     As String
Dim sNums()     As String
Dim iNum        As Integer
Dim bInRange    As Boolean

sInput = "1, 2, 3, 4, 8, 9, 14, 16, 17 ,18 "

sNums = Split(Replace(sInput, " ", ""), ",")
'sOutput = sNums(LBound(sNums))

For iNum = LBound(sNums) To UBound(sNums) - 1

If Val(sNums(iNum + 1)) = Val(sNums(iNum)) + 1 Then

If Not bInRange Then
If Right(sOutput, 2) = ", " Or sOutput = "" Then
sOutput = sOutput & sNums(iNum)
Else
sOutput = sOutput & ", " & sNums(iNum)
End If
End If

bInRange = True

ElseIf bInRange Then

sOutput = sOutput & " - " & sNums(iNum) & ", "
bInRange = False

Else

If Right(sOutput, 2) = ", " Then
sOutput = sOutput & sNums(iNum)
Else
sOutput = sOutput & ", " & sNums(iNum)
End If

End If

Next

If bInRange Then
sOutput = sOutput & " - " & sNums(UBound(sNums))
Else
If Right(sOutput, 2) = ", " Then
sOutput = sOutput & sNums(iNum)
Else
sOutput = sOutput & ", " & sNums(iNum)
End If
End If

Debug.Print sOutput

End Sub
0

LVL 4

Author Comment

Looks good to me...

Doesn't need to be optimised, just work well!!!

Thanks for the help,

James
0

LVL 5

Expert Comment

The code above did a comma at the beginning if the first number wasn't a "range" so ....

Public Sub test()

Dim sInput      As String
Dim sOutput     As String
Dim sNums()     As String
Dim iNum        As Integer
Dim bInRange    As Boolean

sInput = "1, 3, 4, 8, 9, 14, 15, 16, 17 ,18 ,20"

sNums = Split(Replace(sInput, " ", ""), ",")
'sOutput = sNums(LBound(sNums))

For iNum = LBound(sNums) To UBound(sNums) - 1

If Val(sNums(iNum + 1)) = Val(sNums(iNum)) + 1 Then

If Not bInRange Then
If Right(sOutput, 2) = ", " Or sOutput = "" Then
sOutput = sOutput & sNums(iNum)
Else
sOutput = sOutput & ", " & sNums(iNum)
End If
End If

bInRange = True

ElseIf bInRange Then

sOutput = sOutput & " - " & sNums(iNum) & ", "
bInRange = False

Else

If Right(sOutput, 2) = ", " Or sOutput = "" Then
sOutput = sOutput & sNums(iNum)
Else
sOutput = sOutput & ", " & sNums(iNum)
End If

End If

Next

If bInRange Then
sOutput = sOutput & " - " & sNums(UBound(sNums))
Else
If Right(sOutput, 2) = ", " Then
sOutput = sOutput & sNums(iNum)
Else
sOutput = sOutput & ", " & sNums(iNum)
End If
End If

Debug.Print sOutput

End Sub

This one fixes that.
0

## Join & Write a Comment Already a member? Login.

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

#### 734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

#### Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!