Jack
Jack

Reputation: 275

VBA Sort A-Z on One Column

How do I sort all my rows which I have A-Z based on ascending A-Z on column M I have tried the following but it hasn't worked any idea what I might be doing wrong?

Range("A1", Range("Z" & Rows.Count).End(xlUp)).Sort [M1], xlAscending

Upvotes: 2

Views: 8323

Answers (1)

Saud
Saud

Reputation: 490

Record a macro where you select the column you want to be sorted. As an example, I've recorded a macro that sorts column M in ascending order:

Sub SortAsc2()

Dim LastRow As Long

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

Columns("M:M").Select
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("M1"), _
    SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
    .SetRange Range("M1:M" & LastRow)
    .Header = xlGuess
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With

End Sub

Upvotes: 2

Related Questions