# sorting strings

#### kdt2006

##### Board Regular
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

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.

#### Lewiy

##### Well-known Member
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.

#### kdt2006

##### Board Regular
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

#### Lewiy

##### Well-known Member
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!

#### kdt2006

##### Board Regular
Hey Lewiy,

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

Cheers

Replies
3
Views
166
Replies
6
Views
182
Replies
0
Views
337
Replies
8
Views
344
Replies
3
Views
236

1,191,349
Messages
5,986,147
Members
440,005
Latest member
tombamber

### 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.

### Which adblocker are you using?

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

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