sorting data in string

  • Thread starter Thread starter Mark
  • Start date Start date
M

Mark

Hi,
How sort data (variable), which add to string.

E.G.:

value of k =
"1-zzz"
"2-zzz"
"3-zzz"
"4-zzz"

Sub sort()
Dim d As String
For k = 1 To 200
d = Cells(k, 1)
d = d & Cells(k, 1)
End Sub

Code above work until data is sorted in column.
and if data is unsorted it doesn't work.
Is there an easy solution?

Regards
Mark
 
one way:

Public Sub SortK()
Dim d As String
Dim k As Variant
Dim temp As Variant
Dim i As Long
Dim changed As Boolean

k = Range("A1:A200").Value
Do
changed = False
For i = LBound(k, 1) + 1 To UBound(k, 1)
If k(i, 1) < k(i - 1, 1) Then
temp = k(i, 1)
k(i, 1) = k(i - 1, 1)
k(i - 1, 1) = temp
changed = True
End If
Next i
Loop Until Not changed
For i = LBound(k, 1) To UBound(k, 1)
d = d & k(i, 1)
Next i
MsgBox d
End Sub


A more efficient sorting routine would be appropriate if you have
more data.
 
Back
Top