sorting strings

kdt2006

Board Regular
Joined
Apr 6, 2006
Messages
88
Hi,

In column 1 I have some sequences. In column 2 I want to have the alphabetically sorted values for each of the column 1 cells.

Is there a vba sort function I can use for this, or do I have to implement a sorting algorithm such as bubble or insertion sort?

Cheers
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Have you tried using the macro recorder to record yourself performing a sort? That will give you a template of the VBA function required for sorting.
 
Upvote 0
Hi Lewiy,

I can't (or don't know how to) perform a sort as I want to sort the contents of each cell and not sort the column.

for example, I want to get the following output

Column 1: Column 2:
IVQENNGAV AEGINNQVV
VLLLVVVMM LLLMMVVVV
MACLVPAAT AAACLMPTV

With the absence of a specific sort function for strings in vba, the only way I can think of doing this, is by extracting the value from column 1 and using an algorithm to sort the string.

I really hope there is a built in function as I'm a bit short on time :(
 
Upvote 0
Here is an example of a sort which will convert A1 alphabetically and save it in B1. There’s probably a simpler way of doing it but the bubble sort works for me!

Code:
Sub SortMe()
Dim c As Long
Dim d As Long
Dim TempStore As String
Dim myLength As Long
Dim SortArray() As String
myLength = Len(Range("A1"))
ReDim SortArray(myLength)
For c = 1 To myLength
    SortArray(c) = Mid(Range("A1"), c, 1)
Next c
For d = 1 To myLength
    For c = 1 To myLength - 1
        If SortArray(c) > SortArray(c + 1) Then
            TempStore = SortArray(c)
            SortArray(c) = SortArray(c + 1)
            SortArray(c + 1) = TempStore
        End If
    Next c
Next d
Range("B1") = ""
For c = 1 To myLength
    Range("B1") = Range("B1") & SortArray(c)
Next c
End Sub
Hope that helps!
 
Upvote 0
Hey Lewiy,

Thank you soo much. This works great as is pretty fast for 2000 records.

Cheers
 
Upvote 0

Forum statistics

Threads
1,214,872
Messages
6,122,026
Members
449,061
Latest member
TheRealJoaquin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top