Manolo Dominguez Becerra
Manolo Dominguez Becerra

Reputation: 1357

Sorting alphabetically two columns, first one contains a set of repeat values and the second are unique values

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

Answers (2)

VBasic2008
VBasic2008

Reputation: 54807

Sort Multiple Columns

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

navafolk
navafolk

Reputation: 127

Is it excel? Can't you use excel Data -> Sort? enter image description here

It will sort by priority, upper line, upper priority, then next line... will not modify prior sorted line(s).

Upvotes: 0

Related Questions