Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Sorting the contents of a text string in Excel in Alphabetical order.

Posted on 2016-10-04
8
Medium Priority
?
123 Views
Last Modified: 2016-10-13
Hello Gurus,

I have the need to sort a text string in alphabetical order.  (I would prefer a formula based solution if possible.)

Contents in cell A1:  A123,B456,C789
Desired Output:     ,,123456789ABC
As far as the order of the output, I don't really care.  Could be Numbers, Letters, Special Characters or any mix therein...

Can this be done?

Thank you.
0
Comment
Question by:Blistered Pawn
  • 3
  • 3
  • 2
8 Comments
 
LVL 34

Assisted Solution

by:Rob Henson
Rob Henson earned 1000 total points
ID: 41828216
See attached file.

I am sure this could be achieved without so many helper columns but this does what you want.

Columns B to O split the string into individual characters and converts each to its ASCII code equivalent. Columns Q to AD then use the SMALL function to put the codes from B to O into numerical order and convert them back to text characters. The concatenation in AF then combines them back together as one string.

No doubt you will have strings that are longer than 14 characters. Insert columns at column P and AE and copy across from previous columns. Then include the extra cells after AE in the formula in AF.

If you find out which is your longest string, you can insert sufficient columns to accommodate the longest as the formulas allow for shorter strings.

Can I ask why you want this???

Thanks
Rob H
Cell-sort.xlsx
0
 

Author Comment

by:Blistered Pawn
ID: 41828299
Hello Rob H.  Thank you for your response.

The need for this came from a problem that I posed in this post:  Index/Match Question

Your solution does accomplish the goal.  However, I have some text strings that are LEN=305+ characters in length.

The reason I want this is because I have a spreadsheet which contains Bill of Material data which quantities are suspect of being incorrect.

Sheet1 Columns (Model, ChildPN, Qty, DataPoint)       (No ParentPN column)
Sheet2 Columns (ParentPN, ChildPN, Qty, DataPoint)  (No Model column)

Trying to find a way to join the data to pull in ParentPN to Sheet1 and Model to Sheet2 has been challenging.  I have created helper columns for an INDEX/MATCH formula, but it has proven to be inaccurate.

While Sheet1 quantities are suspect, the DataPoint fields are out of order between Sheet1 and Sheet 2, but the data is the same.  Sorting these fields in alphabetical will provide a unique helper column to do my index/match formula.  There is likely a much better way, but I don't know how to get there quickly.

Thank you.  While your solution does the trick, can you think of a way to do this without all the needed helper fields?

I appreciate your help.
0
 
LVL 34

Expert Comment

by:Rob Henson
ID: 41829597
I have been looking at using a User Defined Function.

I can get the characters from the cell into a comma separated array but I can't find a way of then sorting that array. Still working on it though!!
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 46

Assisted Solution

by:aikimark
aikimark earned 1000 total points
ID: 41829745
This is a type of bucket sort on the string's characters.
Public Function CollatedChars(ByVal parmString As String) As String
    Dim lngBuckets(0 To 255) As Long
    Dim lngLoop As Long
    Dim bChar As Byte
    For lngLoop = 1 To Len(parmString)
        bChar = Asc(Mid(parmString, lngLoop, 1))
        lngBuckets(bChar) = lngBuckets(bChar) + 1
    Next
    CollatedChars = vbNullString
    For lngLoop = LBound(lngBuckets) To UBound(lngBuckets)
        If lngBuckets(lngLoop) <> 0 Then
            CollatedChars = CollatedChars & String(lngBuckets(lngLoop), Chr(lngLoop))
        End If
    Next
End Function

Open in new window

0
 
LVL 34

Assisted Solution

by:Rob Henson
Rob Henson earned 1000 total points
ID: 41829854
I reached out to other EE members for help:

https://www.experts-exchange.com/questions/28974440/Storing-values-to-an-array.html

With their help we came up with this combination of UDF and Macro:
Function SortChar(c As Range)
Dim Str As String
Dim MyString As Variant

L = Len(c.Value)
T = c.Value

For P = 1 To L
    Sep = ","
    If P = L Then Sep = ""
    NextVal = Mid(T, P, 1) & Sep
    Str = Str & NextVal
Next P

MyString = Split(Str, ",")
QuickSort MyString, LBound(MyString), UBound(MyString)
Ch = MyString(0)
SortChar = Join(MyString, "")
End Function

Sub QuickSort(arr, Lo As Long, Hi As Long)
  Dim varPivot As Variant
  Dim varTmp As Variant
  Dim tmpLow As Long
  Dim tmpHi As Long
  tmpLow = Lo
  tmpHi = Hi
  varPivot = arr((Lo + Hi) \ 2)
  Do While tmpLow <= tmpHi
    Do While arr(tmpLow) < varPivot And tmpLow < Hi
      tmpLow = tmpLow + 1
    Loop
    Do While varPivot < arr(tmpHi) And tmpHi > Lo
      tmpHi = tmpHi - 1
    Loop
    If tmpLow <= tmpHi Then
      varTmp = arr(tmpLow)
      arr(tmpLow) = arr(tmpHi)
      arr(tmpHi) = varTmp
      tmpLow = tmpLow + 1
      tmpHi = tmpHi - 1
    End If
  Loop
  If Lo < tmpHi Then QuickSort arr, Lo, tmpHi
  If tmpLow < Hi Then QuickSort arr, tmpLow, Hi
End Sub

Open in new window


Credit to Rgonzo1971 for the QuickSort sub and assistance from Saquib and Neeraj for correcting the array implications of the SortChar function.

To use either my or Aikimark's function solution, use the following in a cell:

=SortChar(CellReference)
=CollatedChars(CellReference)

where CellReference contains the string of text to be sorted.

A comment from RGonzo regarding Aikimark's code is that it will only work on Ascii code and not Unicode.

Thanks
Rob H
0
 
LVL 46

Expert Comment

by:aikimark
ID: 41829902
If the string has unicode characters, then the buckets array would increase to 64K.  I would probably use the StrConv() function instead of the Asc().  Alternatively, I could use the AscW() and ChrW() functions.
0
 
LVL 46

Accepted Solution

by:
aikimark earned 1000 total points
ID: 41830187
Here's a unicode-friendly version of the function:
Public Function CollatedCharsUnicode(ByVal parmString As String) As String
    Dim lngBuckets(0 To 65535) As Long
    Dim lngLoop As Long
    Dim bChar As Long
    
    For lngLoop = 1 To Len(parmString)
        bChar = AscW(Mid(parmString, lngLoop, 1))
        lngBuckets(bChar) = lngBuckets(bChar) + 1
    Next
    CollatedCharsUnicode = vbNullString
    For lngLoop = LBound(lngBuckets) To UBound(lngBuckets)
        If lngBuckets(lngLoop) <> 0 Then
            CollatedCharsUnicode = CollatedCharsUnicode & String(lngBuckets(lngLoop), ChrW(lngLoop))
        End If
    Next
End Function

Open in new window

0
 

Author Closing Comment

by:Blistered Pawn
ID: 41841890
Thanks to everyone for your assistance.
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question