Hi Group
Trying to sort a string looking like:
bv-8,ok-3,bv-5,sk-1,bh-2,ok-9 etc
into:
bh-2,bv-5,bv-8,ok-3,ok-9,sk-1 etc
Grateful for some help.
Brgds
CG Rosen
|
|
0
|
|
|
|
Reply
|
CG
|
1/3/2010 10:41:31 AM |
|
Try This code.
Sub SortData()
InputStr = "bv-8,ok-3,bv-5,sk-1,bh-2,ok-9"
'put data into an array
SortArray = Split(InputStr, ",")
For I = LBound(SortArray) To (UBound(SortArray) - 1)
For J = (I + 1) To UBound(SortArray)
If SortArray(I) > SortArray(J) Then
temp = SortArray(I)
SortArray(I) = SortArray(J)
SortArray(J) = temp
End If
Next J
Next I
OutputStr = Join(SortArray, ",")
End Sub
--
joel
------------------------------------------------------------------------
joel's Profile: 229
View this thread: http://www.thecodecage.com/forumz/showthread.php?t=166528
[url="http://www.thecodecage.com"]Microsoft Office Help[/url]
|
|
0
|
|
|
|
Reply
|
joel
|
1/3/2010 11:33:34 AM
|
|
Split the string into an array
ary = Split(sData, ",")
Drop that array onto a worksheet
Set rng = Range("A1").Resize(UBound(ary) - LBound(ary) + 1)
rng = Application.Transpose(ary)
Sort the range
rng.Sort key1:=Range("A1"), order1:=xlAscending, header:=xlNo
Pull that range back into the array
ary = Application.Transpose(rng)
Then join the array back into the string
sData = Join(ary, ",")
"CG Rosen" <carlgran.rosen@telia.com> wrote in message
news:23056155-A249-4AAC-8A78-620753C038C6@microsoft.com...
> Hi Group
>
> Trying to sort a string looking like:
>
> bv-8,ok-3,bv-5,sk-1,bh-2,ok-9 etc
>
> into:
>
> bh-2,bv-5,bv-8,ok-3,ok-9,sk-1 etc
>
> Grateful for some help.
>
> Brgds
>
> CG Rosen
|
|
0
|
|
|
|
Reply
|
Bob
|
1/3/2010 11:38:16 AM
|
|
Hi CG
Sub test()
MsgBox SortString("bv-8,ok-3,bv-5,sk-1,bh-2,ok-9", ",")
End Sub
Function SortString(S As String, Separator As String) As String
Dim X() As String
Dim tmp As String
Dim i As Long, j As Long
'split:
X = Split(S, ",")
'trim:
For i = LBound(X) To UBound(X)
X(i) = Trim$(X(i))
Next
'sort:
For i = LBound(X) To UBound(X) - 1
For j = LBound(X) To UBound(X) - 1
If X(j) > X(j + 1) Then
tmp = X(j + 1)
X(j + 1) = X(j)
X(j) = tmp
End If
Next
Next
'rebuild:
tmp = ""
For i = LBound(X) To UBound(X)
tmp = tmp & X(i) & ","
Next
tmp = Left(tmp, Len(tmp) - 1)
SortString = tmp
End Function
HTH. Best wishes Harald
"CG Rosen" <carlgran.rosen@telia.com> wrote in message
news:23056155-A249-4AAC-8A78-620753C038C6@microsoft.com...
> Hi Group
>
> Trying to sort a string looking like:
>
> bv-8,ok-3,bv-5,sk-1,bh-2,ok-9 etc
>
> into:
>
> bh-2,bv-5,bv-8,ok-3,ok-9,sk-1 etc
>
> Grateful for some help.
>
> Brgds
>
> CG Rosen
|
|
0
|
|
|
|
Reply
|
Harald
|
1/3/2010 11:39:04 AM
|
|
Hi,
This works on A1 in the activesheet. It cheats by copying the string to a
new range, sorting it then putting it back. It uses column IV as the
temporary sort range so any data in that column will be lost.
Sub Sonic()
Dim x As Long, LastRow As Long
Dim V As Variant
Dim S As String, newstring As String
Columns(256).ClearContents
S = Range("A1").Value
V = Split(S, ",")
Application.ScreenUpdating = False
For x = 0 To UBound(V)
Cells(x + 1, 256).Value = V(x)
Next
LastRow = Cells(Cells.Rows.Count, "IV").End(xlUp).Row
Worksheets("Sheet1").Sort.SortFields.Clear
Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("IV1"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("IV1:IV" & LastRow)
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Set MyRange = Range("IV1:IV" & LastRow)
For Each c In MyRange
newstring = newstring & c.Value & ","
Next
Range("a1").Value = Left(newstring, Len(newstring) - 1)
Columns(256).ClearContents
Application.ScreenUpdating = True
End Sub
Mike
"CG Rosen" wrote:
> Hi Group
>
> Trying to sort a string looking like:
>
> bv-8,ok-3,bv-5,sk-1,bh-2,ok-9 etc
>
> into:
>
> bh-2,bv-5,bv-8,ok-3,ok-9,sk-1 etc
>
> Grateful for some help.
>
> Brgds
>
> CG Rosen
|
|
0
|
|
|
|
Reply
|
Utf
|
1/3/2010 11:47:01 AM
|
|
And here's another one, spoilt for choice !
Sub test()
Dim S As String, sorted As String
S = "bv-8,ok-3,bv-5,sk-1,bh-2,ok-9"
sorted = SortCommaSepString(S)
Debug.Print sorted ' bh-2,bv-5,bv-8,ok-3,ok-9,sk-1
End Sub
Function SortCommaSepString(ByVal strIn) As String
Dim i As Long, j As Long
Dim s1 As String, s2 As String
Dim arr() As String
arr = Split(strIn, ",")
For i = 0 To UBound(arr) - 1
For j = (i + 1) To UBound(arr)
s1 = arr(i)
s2 = arr(j)
If StrComp(s1, s2, vbTextCompare) = 1 Then
arr(i) = s2
arr(j) = s1
End If
Next j
Next i
SortCommaSepString = Join(arr, ",")
End Function
All posted examples will give similar results with the test sample. However
with different strings the results might be very different, as three
different comparison methods are used. Bob's will sort the way Excel does
though it means using cells. Otherwise for most string comparisons it's
better (I think) to use the StrComp function rather than simple greater/less
comparisons.
Regards,
Peter T
"CG Rosen" <carlgran.rosen@telia.com> wrote in message
news:23056155-A249-4AAC-8A78-620753C038C6@microsoft.com...
> Hi Group
>
> Trying to sort a string looking like:
>
> bv-8,ok-3,bv-5,sk-1,bh-2,ok-9 etc
>
> into:
>
> bh-2,bv-5,bv-8,ok-3,ok-9,sk-1 etc
>
> Grateful for some help.
>
> Brgds
>
> CG Rosen
|
|
0
|
|
|
|
Reply
|
Peter
|
1/3/2010 12:01:54 PM
|
|
"CG Rosen" <carlgran.rosen@telia.com> wrote in message
news:23056155-A249-4AAC-8A78-620753C038C6@microsoft.com...
> Hi Group
>
> Trying to sort a string looking like:
>
> bv-8,ok-3,bv-5,sk-1,bh-2,ok-9 etc
>
> into:
>
> bh-2,bv-5,bv-8,ok-3,ok-9,sk-1 etc
>
> Grateful for some help.
>
> Brgds
>
> CG Rosen
Try this macro:
Sub sort_string(source As Range, destination As Range)
Dim parts() As String
Dim sorted As String
parts = Split(source.Value, ",")
imax = UBound(parts)
For i = 0 To imax - 1
For j = i + 1 To imax
If parts(j) < parts(i) Then
tmp = parts(i)
parts(i) = parts(j)
parts(j) = tmp
End If
Next j
Next i
sorted = ""
For i = 0 To imax - 1
sorted = sorted & parts(i) & ","
Next i
sorted = sorted & parts(imax)
destination.Value = sorted
End Sub
Example:
sort_string Range("A1"), Range("B4")
will sort the string in cell A1 and put the result in cell B4
Hope this helps / Lars-�ke
|
|
0
|
|
|
|
Reply
|
iso
|
1/3/2010 12:02:20 PM
|
|
Hi All,
Thanks for your suggestions. I will
test all of them and learn more.
Brgds
CG Rosen
"CG Rosen" <carlgran.rosen@telia.com> skrev i meddelandet
news:23056155-A249-4AAC-8A78-620753C038C6@microsoft.com...
> Hi Group
>
> Trying to sort a string looking like:
>
> bv-8,ok-3,bv-5,sk-1,bh-2,ok-9 etc
>
> into:
>
> bh-2,bv-5,bv-8,ok-3,ok-9,sk-1 etc
>
> Grateful for some help.
>
> Brgds
>
> CG Rosen
>
|
|
0
|
|
|
|
Reply
|
CG
|
1/3/2010 2:00:08 PM
|
|
|
7 Replies
165 Views
(page loaded in 2.319 seconds)
Similiar Articles: How to sort List<...> -- created using LinQ? - microsoft ...c# - Sorting a list using Lambda/Linq to objects - Stack Overflow I have the name of the "sort by property" in a string. I will need to use Lambda/Linq to sort the list of ... Converting a string into a filename - microsoft.public.mac.office ...How to Sort on Date in Filename? - microsoft.public.scripting ... >> But I had ... In this example we are going to convert String into date. Convert String to Date ... Sorting cells with a string formulas - microsoft.public.excel ...In trying to split addresses which contain both number and street in one cell, I have extracted using these formulas: =REPLACE(B5,1,FIND(" ",B5,1)... How to test that a string can be represented by a font - microsoft ...If a particular string contains characters that cannot be represented in that font ... To make your code fast you should implement some sort of caching (i.e. a list of ... How to Sort a recorset and then Seek in that recordset (ADO ...ADO Sort Property The Sort property sets or returns a string value that specifies the field names in the Recordset to sort on. Each field name must be separated by a comma. Sorting comma strings - microsoft.public.word.docmanagement ...We frequently work with strings of numbers separated by commas (e.g., 6, 55, 8, 341, 21, 4). We sort those numbers by selecting them, replacing comm... Requery sort order on a Form - microsoft.public.accessDataBase - Requery not working after changing ORDER by in query SQL string ... BY " Then in respond to a command button on the form, I want to sort by ... How do I left justify a date - microsoft.public.access.queries ...DavidSherwood wrote: > If I convert the date to string in a query, it won't sort properly Your problem stems from using the Query Builder to display results to users. String formula conversion - microsoft.public.excel.misc ...I wish to do an advanced Sort & Filter against a list of names not generated by a ... This is a discussion on Formula string conversion within the C++ Programming forums ... Showing/Using Sort arrows on vb6 listviews? - microsoft.public.vb ...... of the listview, but I could never figure out how to show the up or down sort ... LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As ... How to Sort a String Array in VBA | eHow.comSorting a string array in Visual Basic for Applications (VBA) is not as simple as in other, more recent programming languages. In VBA, you can sort an array in ... How to Sort a Unicode String in Ruby | eHow.comThe Ruby programming language has many built-in functions that allow you to quickly accomplish general programming tasks. You can use these built-in functions to sort ... Java array sorting example - how to sort a Java String array ...Java String array sorting tip - How to use the Java Arrays class to sort a Java String array. Sorting data in Java: how to sort a list of Strings or IntegersFor example, we might want to perform a case insensitive sort on Strings, or perform a "proper" alphabetic sort that takes account of things like the correct ordering of ... string sort java, String sort() Method - JSP Tutorials,EJB ...In this program you will learn how to sort words in a String. The sorting will be done in ascending order. String sort() Method In this program you will ... 7/17/2012 6:13:54 PM
|