Rearrange a cell with multiple values alphabetically [Excel]

时间:2016-04-25 09:09:30

标签: excel excel-vba vba

I have an Excel file where I have a cell with multiple values, split by a comma. For instance: New York, Chicago, Los Angeles. Now I want to rearrange this cell in alphatical order, like: Chicago, Los Angeles, New York. Is there an easy way to do this?

4 个答案:

答案 0 :(得分:2)

试试这个。以下代码将读取列A中的值,并在列B中提供所需的结果。

Sub SortString()
    Dim MyArray As Variant, varSwap As Variant
    Dim i As Long, min As Long, max As Long, LastRow As Long
    Dim str As String
    Dim MyRange As Range
    Dim IsSwapped As Boolean

    LastRow = Cells(Rows.Count, "A").End(xlUp).Row

    Set MyRange = Range("A1:A" & LastRow)
    For Each cell In MyRange
        MyArray = Split(cell.Value, ",")

        min = LBound(MyArray)
        max = UBound(MyArray) - 1
        Do
            IsSwapped = False
            For i = min To max
                If MyArray(i) > MyArray(i + 1) Then
                    varSwap = MyArray(i)
                    MyArray(i) = MyArray(i + 1)
                    MyArray(i + 1) = varSwap
                    IsSwapped = True
                End If
            Next
            max = max - 1
        Loop Until Not IsSwapped

        For i = LBound(MyArray) To UBound(MyArray)
            Debug.Print MyArray(i)
            If str = "" Then
                str = Trim(MyArray(i))
            Else
                str = str & ", " & Trim(MyArray(i))
            End If
        Next i
        cell.Offset(0, 1).Value = str
        str = ""
    Next cell
End Sub

答案 1 :(得分:0)

Assuming your data is in A1, use text to columns with a delimited width and select the comma as your separator. When the cells are split, select them all and sort alphabetically. Then in a different cell use,

=A1 & ", " & B1 & ", " C1

And so on. If you want this in VBA (as per your tag) a good starting point will be to do the above whilst recording a macro, then edit to your needs.

EDIT

This is by no means pretty, but its a starting point to use VBA to run through each row.

Dim i As Integer, lastRow As Integer, curRng As Range

lastRow = Range("A10000").End(xlUp).Row

For i = 1 To lastRow
Cells(i, 1).Select
Set curRng = Range(ActiveCell, ActiveCell.End(xlToRight))
    curRng.Select
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=curRng, _
    SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange curRng
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlLeftToRight
        .SortMethod = xlPinYin
        .Apply
    End With
Next i

答案 2 :(得分:0)

Use the SPLIT function to first store your comma separated value list in an array, then sort the array in any way you wish, then use the JOIN function to create a comma separated value list again.

For example:

myArray = SPLIT(Cell.Value, ",")
Call SomeSortFunction(myArray)
myString = JOIN(myArray, ",")
Cell.Value = myString

答案 3 :(得分:0)

You need to save the string of values from the cell into a function and then loop through the list splitting on comma:

Array = Split(yourString, ",")

Then loop through it:

For i = 0 To UBound(Array)

To find the first letter of each value I would use something like:

Left(Array(i), Len(Array(i)) - 1)

You can then sort it alphabetically. You could use SUBSTITUTE() function to swap values around.