Sort range ascending

verluc

Well-known Member
Joined
Mar 1, 2002
Messages
1,451
Is the following possible in Excel:

Select range : A4 to last row and R4 to last row
Then sort this range ascending with column D
Many thanks in advance for any help.
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Andrew,

This is the macro I get with the macro recorder.The problem is that the row to R247 change every day.
So to the "last row" in R is much better.

Sub Macro1()
Range("A4:R247").Select
ActiveWorkbook.Worksheets("Data").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Data").Sort.SortFields.Add Key:=Range _
("D4:D247"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("Data").Sort
.SetRange Range("A4:R247")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
 
Upvote 0
Try:

Code:
Sub Macro1()
    Dim LR As Long
    With ActiveWorkbook.Worksheets("Data")
        LR = .Range("A" & .Rows.Count).End(xlUp).Row
        .Sort.SortFields.Clear
        .Sort.SortFields.Add Key:=.Range _
            ("D4:D" & LR), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortNormal
        With .Sort
            .SetRange .Parent.Range("A4:R" & LR)
            .Header = xlGuess
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    End With
End Sub

When posting VBA code please use Code tags.
 
Upvote 0
Million thanks, Andrew, it works like I wanted !!!

(where can I add the Code tags ?)
 
Upvote 0
Andrew,

A little additional question : on my excel program 2010 it works excellent, but on an excel version 2003 I get
with this macro the error 438 on the field : .Sort.SortFields.Clear
Any idea?
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,715
Members
448,985
Latest member
chocbudda

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