Recorded sort macro doesn't work

kgkev

Well-known Member
Joined
Jun 24, 2008
Messages
1,285
Office Version
  1. 365
Platform
  1. Windows
I recorded a simple sort macro

Code:
Sub datasort()
' datasort Macro
'Sorts data in the master sheet - Called from main Get Data button
'
    Range("A3").Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.Sort Key1:=Range("A4"), Order1:=xlAscending, Header:=xlGuess, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
    Range("A8").Select
    Selection.End(xlDown).Select
    Selection.End(xlDown).Select
    Selection.End(xlDown).Select
    Selection.End(xlDown).Select
    Selection.End(xlUp).Select
    End Sub

First off it doesn't work and I can't see why - If I manually do a very similar set of key strokes it sorts without a problem but running the macro fails.

Secondly my data starts at A3 (header) and continues for a variable amount of time. there is a chance that there are gaps which is why I have added the additional "Selection.End(xlDown).Select" but I figure there must be a more efficient method of doing this.
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
if you're only sorting A

Code:
Dim rng As Range
Set rng = Range(Cells(3, 1), Cells(Rows.Count, 1).End(xlUp))
rng.Sort Key1:=Cells(4, 1), Order1:=xlAscending, Header:=xlYes, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
 
Upvote 0
Because you may have blanks it hard to know the best way to ensure all rows A:T are picked up without using last cell of entire worksheet... so would advise something like the below... before running create a copy of the sheet you wish to run this against (just in case!)

Code:
Sub Sort()
Dim rng As Range
Set rng = Range(Cells(3, 1), Cells(3, 1).SpecialCells(xlCellTypeLastCell))
If rng.Columns.Count > 20 Then
    Set rng = rng.Resize(, rng.Columns.Count - (rng.Columns.Count - 20))
End If
rng.Sort Key1:=Cells(4, 1), Order1:=xlAscending, Header:=xlYes, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
End Sub
 
Upvote 0
I found a slightly easier way

I wrote a nasty email to the person who keeps leaving blank rows in his spreadsheet and told him I would withold his commision payments if it carries on.

Thanks for your help though
 
Upvote 0

Forum statistics

Threads
1,214,583
Messages
6,120,383
Members
448,955
Latest member
BatCoder

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