error on sorting a range

R

RocketRod

Hi
I have an error happening when I run this Sort on a named range called
"DataRange" on the worksheet tab "Claim"

The message s
"Run time 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"

The code is

Application.Goto Reference:="DataRange"
ActiveWorkbook.Worksheets("Claim").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Claim").Sort.SortFields.Add
Key:=Range("DataRange"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Claim").Sort
.SetRange Range("DataRange")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

The Debug highlights the .Apply line.
When I step through the program the range is selected OK.
I recorded the code by selecting Goto button and selecting DataRange but
changed the cell reference from the B3:J60 notation to DataRange in the macro.


Correction suggestions please
 
L

Lars-Åke Aspelin

Hi
I have an error happening when I run this Sort on a named range called
"DataRange" on the worksheet tab "Claim"

The message s
"Run time 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"

The code is

Application.Goto Reference:="DataRange"
ActiveWorkbook.Worksheets("Claim").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Claim").Sort.SortFields.Add
Key:=Range("DataRange"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Claim").Sort
.SetRange Range("DataRange")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

The Debug highlights the .Apply line.
When I step through the program the range is selected OK.
I recorded the code by selecting Goto button and selecting DataRange but
changed the cell reference from the B3:J60 notation to DataRange in the macro.


Correction suggestions please


Try changing
Key:=Range("DataRange")
to
Key:=Range("DataRange")(,n)
where n is the number of the column, within the range that you want
the range sorted by,

Key:=Range("DataRange")(,1)
if you want the data to be sort by the first column, column B in your
example.

Hope this helps / Lars-Åke
 
R

RocketRod

Thank you - that worked

Lars-Ã…ke Aspelin said:
Try changing
Key:=Range("DataRange")
to
Key:=Range("DataRange")(,n)
where n is the number of the column, within the range that you want
the range sorted by,

Key:=Range("DataRange")(,1)
if you want the data to be sort by the first column, column B in your
example.

Hope this helps / Lars-Ã…ke
.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Applying Variables to SORT 4
Undo Macro Action 3
VBA 2 Codes 2
Clear Check Box 2
Sorting Question 5
Sort by one column then another. 2
Problem with Worksheet Activate and Sorting 7
Sort Macro Compatibility 3

Top