ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   alphabetize within a cell (https://www.excelbanter.com/excel-programming/406143-alphabetize-within-cell.html)

[email protected]

alphabetize within a cell
 
if i have a list of letters in a cell, is it possible to alphabetize
them?

example: ADCB -- ABCD

Mike H

alphabetize within a cell
 
Hi,

This will sort A1 into A2

Sub gg()
x = Len(Cells(1, 1))
Cells(2, 1).ClearContents
For i = 1 To x
Cells(i, 2) = Mid(Cells(1, 1), i, 1)
Next i
Cells(1, 2).Resize(x).Sort Key1:=Cells(1, 2), Order1:=xlAscending, _
Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
For i = 1 To x
Cells(2, 1) = Cells(2, 1) & Cells(i, 2)
Cells(i, 2).ClearContents
Next i
End Sub

Mike

" wrote:

if i have a list of letters in a cell, is it possible to alphabetize
them?

example: ADCB -- ABCD


[email protected]

alphabetize within a cell
 
On Feb 14, 1:13 pm, Mike H wrote:
Hi,

This will sort A1 into A2

Sub gg()
x = Len(Cells(1, 1))
Cells(2, 1).ClearContents
For i = 1 To x
Cells(i, 2) = Mid(Cells(1, 1), i, 1)
Next i
Cells(1, 2).Resize(x).Sort Key1:=Cells(1, 2), Order1:=xlAscending, _
Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
For i = 1 To x
Cells(2, 1) = Cells(2, 1) & Cells(i, 2)
Cells(i, 2).ClearContents
Next i
End Sub

Mike

" wrote:
if i have a list of letters in a cell, is it possible to alphabetize
them?


example: ADCB -- ABCD


Is there a way that this can work in any cell, first of all.

second, can it Change ABaD -- AaBD and not ABDa.

third, can it output to the same cell as the input
if not, could it go to an identical position (ex: D7) on a different
worksheet

Peter T

alphabetize within a cell
 
You can use the following as a UDF, like this
=SortLetters(A1)

Option Explicit
'' to sort like a=A use Option Compare Text
' or to sort like ABCabc don't

Option Compare Text

Function SortLetters(v As Variant) As String
Dim bFlag As Boolean
Dim n As Long, i As Long
Dim s As String

n = Len(v)
If n = 0 Then Exit Function

ReDim arrS(0 To Len(v))
For i = 1 To Len(v)
arrS(i) = Mid$(v, i, 1)
Next

Do
bFlag = True
For i = LBound(arrS) To UBound(arrS) - 1
If arrS(i) arrS(i + 1) Then
bFlag = False
arrS(0) = arrS(i)
arrS(i) = arrS(i + 1)
arrS(i + 1) = arrS(0)
End If
Next i
Loop While Not bFlag

s = ""
For i = 1 To UBound(arrS)
s = s & arrS(i)
Next

SortLetters = s

End Function

Regards,
Peter T

wrote in message
...
if i have a list of letters in a cell, is it possible to alphabetize
them?

example: ADCB -- ABCD





All times are GMT +1. The time now is 10:16 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com