ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Sort Runtime Erroe 1004 (https://www.excelbanter.com/excel-programming/391427-sort-runtime-erroe-1004-a.html)

Sandy

Sort Runtime Erroe 1004
 
I have a simple sort which I have used many times. I recorded it with the
macro recorder and it works fine but when in the code it gets a
'Runtime error 1004'. The sort reference is not valid. Make sure that it's
within the data you want to sort and, the first Sort By box isn't the same
or blank.

Here is the code.

Sheets("Report Log").Select
Sheets("Report Log").Range("A1").Select
Selection.CurrentRegion.Select
Selection.Sort Key1:=Range("N2"), Order1:=xlAscending, Key2:=Range("A2") _
, Order2:=xlAscending, Key3:=Range("B2"),
Order3:=xlAscending, Header:= _
xlGuess, OrderCustom:=1, MatchCase:=False,
Orientation:=xlTopToBottom

Thanmk you


JLGWhiz

Sort Runtime Erroe 1004
 
Hi Sandy, it is hard to tell without seeing your worksheet, but if there is
an empty column between column N and Column A current region the it will
tilt. One other thing is you begin your region with row 1 but set your key
to row 2, that could be confusing the compiler and triggering the error
message.

"Sandy" wrote:

I have a simple sort which I have used many times. I recorded it with the
macro recorder and it works fine but when in the code it gets a
'Runtime error 1004'. The sort reference is not valid. Make sure that it's
within the data you want to sort and, the first Sort By box isn't the same
or blank.

Here is the code.

Sheets("Report Log").Select
Sheets("Report Log").Range("A1").Select
Selection.CurrentRegion.Select
Selection.Sort Key1:=Range("N2"), Order1:=xlAscending, Key2:=Range("A2") _
, Order2:=xlAscending, Key3:=Range("B2"),
Order3:=xlAscending, Header:= _
xlGuess, OrderCustom:=1, MatchCase:=False,
Orientation:=xlTopToBottom

Thanmk you


joel

Sort Runtime Erroe 1004
 
Rather than use a selection use a preset range like my code below

Sub mysort()

Sheets("Report Log").Select
Sheets("Report Log").Activate
Set sortrange = Range("A2:N3")
sortrange.Sort _
Key1:=Range("N2:N3"), Order1:=xlAscending, _
Key2:=Range("A2:A3"), Order2:=xlAscending, _
Key3:=Range("B2:B3"), Order3:=xlAscending, _
Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom
End Sub

"JLGWhiz" wrote:

Hi Sandy, it is hard to tell without seeing your worksheet, but if there is
an empty column between column N and Column A current region the it will
tilt. One other thing is you begin your region with row 1 but set your key
to row 2, that could be confusing the compiler and triggering the error
message.

"Sandy" wrote:

I have a simple sort which I have used many times. I recorded it with the
macro recorder and it works fine but when in the code it gets a
'Runtime error 1004'. The sort reference is not valid. Make sure that it's
within the data you want to sort and, the first Sort By box isn't the same
or blank.

Here is the code.

Sheets("Report Log").Select
Sheets("Report Log").Range("A1").Select
Selection.CurrentRegion.Select
Selection.Sort Key1:=Range("N2"), Order1:=xlAscending, Key2:=Range("A2") _
, Order2:=xlAscending, Key3:=Range("B2"),
Order3:=xlAscending, Header:= _
xlGuess, OrderCustom:=1, MatchCase:=False,
Orientation:=xlTopToBottom

Thanmk you



All times are GMT +1. The time now is 01:14 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com