Sorting Cells (Alphabetical)

Disarmonious

Board Regular
Joined
Oct 31, 2016
Messages
144
I have 6 columns (cells L3 and O3 are labeled "Name", cells M3 and P3 are labeled "ID", and cells N3 and Q3 are labeled "Location"). I have a button named "Sort". I would like to create a macro that would sort the names alphabetically when pressed. I also need the information in the neighboring column/cells to follow the names as they move up/down when sorting. Example;

Johnson, Shirley 12478 Forest Areno, Gilbert 61475 Mountain
Storm, Jack 45164 Mountain Gaston, Alex 33413 Lake
Fredrick, Kenny 10141 Lake Zildan, Gus 41741 Forest

When the "Sort" button is pressed, I want the names to arrange like this;

cells (L3, M3, and N3)

Areno, Gilbert 61475 Mountain
Fredrick, Kenny 10141 Lake
Gaston, Alex 33413 Lake


cells (O3, P3, and Q3)

Johnson, Shirley 12478 Forest
Storm, Jack 45164 Mountain <strike>
</strike>
Zildan, Gus 41741 Forest



Essentially, when the names are sorted alphabetically, their ID and Location follows. Hope this helps...
 
Last edited:

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Code:
Sub Sort()
Dim lr&, rng As Range
lr = Cells(Rows.Count, "L").End(xlUp).Row
Set rng = Range("O2:Q" & lr)
Application.ScreenUpdating = False
rng.Cut Destination:=Cells(lr + 1, "L")
[L:N].Sort Key1:=[L1], Order1:=xlAscending, Header:=xlYes
Range(Cells(lr + 1, "L"), Cells(lr + 1, "N").Resize(lr - 1)).Cut Destination:=[O2]
Application.ScreenUpdating = True
End Sub
 
Upvote 0
.
Using the Macro Recorder created this macro :

Code:
Sub Macro1()
Application.ScreenUpdating = False


    Range("L4:N6").Select
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("L4:L6"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("L3:N6")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    
    Range("O3:Q6").Select
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("O4:O6"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("O3:Q6")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Range("K1").Select
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Similar threads

Forum statistics

Threads
1,223,911
Messages
6,175,323
Members
452,635
Latest member
laura12345

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