Reputation: 1357
How can I sort to columns in the following way
My two columns before sorting
C Ciks
C Bsdjnf
C ACfff
A Bhdh
A Apdp
A Cyay
B Ayay
B Cnan
B Btag
After being sorted
A Apdp
A Bhdh
A Cyay
B Ayay
B Btag
B Cnan
C ACfff
C Bsdjnf
C Ciks
I have been using the common command found for example sort multiple columns excel VBA but when sorting the second column, this modified the first one.
Upvotes: 2
Views: 304
Reputation: 54807
Option Explicit
Sub SortMultipleColumns()
Dim ws As Worksheet: Set ws = ActiveSheet ' improve!
Dim rg As Range: Set rg = ws.Range("A1").CurrentRegion
With ws.Sort
.SortFields.Clear
.SortFields.Add Key:=rg.Columns(1), Order:=xlAscending
.SortFields.Add Key:=rg.Columns(2), Order:=xlAscending
.SetRange rg
.Header = xlNo ' usually you have headers, then use 'xlYes'
.Apply
End With
End Sub
Upvotes: 2
Reputation: 127
Is it excel? Can't you use excel Data -> Sort?
It will sort by priority, upper line, upper priority, then next line... will not modify prior sorted line(s).
Upvotes: 0