Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
RTP RTP is offline
external usenet poster
 
Posts: 4
Default sorting text in a string

How can I sort a string of text in VBA. For Example -

v_text = "gbc3d1aef2"

should be sorted to "abcdefg123"

Cheers
RTP
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default sorting text in a string

On Sat, 25 Aug 2007 10:56:02 -0700, RTP wrote:

How can I sort a string of text in VBA. For Example -

v_text = "gbc3d1aef2"

should be sorted to "abcdefg123"

Cheers
RTP


There are a number of different sort algorithms. Here's one courtesy of MS.

==================================================
Option Explicit
Sub foo()
Const v_text As String = "gbc3d1aef2"
Dim t(), t1
Dim i As Long

ReDim t(Len(v_text) - 1)
For i = 0 To UBound(t)
t(i) = Mid(v_text, i + 1, 1)
Next i

BubbleSort t

For i = 0 To UBound(t)
t1 = t1 & t(i)
Next i

Debug.Print v_text, t1
End Sub
'-------------------------------------------------
Private Sub BubbleSort(TempArray As Variant)
Dim temp As Variant
Dim i As Integer
Dim NoExchanges As Integer

' Loop until no more "exchanges" are made.
Do
NoExchanges = True

' Loop through each element in the array.
For i = 0 To UBound(TempArray) - 1

' If the element is greater than the element
' following it, exchange the two elements.
If TempArray(i) TempArray(i + 1) Then
NoExchanges = False
temp = TempArray(i)
TempArray(i) = TempArray(i + 1)
TempArray(i + 1) = temp
End If
Next i
Loop While Not (NoExchanges)
End Sub
======================================
--ron
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default sorting text in a string

A quick and dirty solution:

Sub SortThisText()
Dim SortedLetters(255) As String

TextToSort = Application.Selection
SortedText = ""

For i = 1 To Len(TextToSort)
CurrentChar = Mid(TextToSort, i, 1)
SortedLetters(Asc(CurrentChar)) = SortedLetters(Asc(CurrentChar))
& CurrentChar
Next

For i = 1 To 255
SortedText = SortedText & SortedLetters(i)
Next

MsgBox (SortedText)
End Sub

This sorts the text based on ASCII codes and pops a message box with
the sorted text. You will need to be careful that ASCII order is what
you want - in your example you wanted numbers to come after letters so
that would need some more logic in the code to achieve that.

HTH
UKMatt

On Aug 25, 10:56 am, RTP wrote:
How can I sort a string of text in VBA. For Example -

v_text = "gbc3d1aef2"

should be sorted to "abcdefg123"

Cheers
RTP



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Change 3 letter text string to a number string Pete Excel Discussion (Misc queries) 3 December 31st 07 07:47 PM
Sorting By Largest Value In A String [email protected] Excel Programming 1 July 6th 06 07:44 PM
Splitting a text string into string and number mcambrose Excel Discussion (Misc queries) 4 February 21st 06 03:47 PM
string sorting problem NikkoW Excel Programming 1 May 2nd 04 04:59 PM
sorting data in string Mark[_17_] Excel Programming 1 September 16th 03 05:08 PM


All times are GMT +1. The time now is 05:45 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"