#1   Report Post  
Posted to microsoft.public.excel.programming
MC MC is offline
external usenet poster
 
Posts: 29
Default Sort error?

I don't know what the difference is between these 2 pieces of code. The
first piece works but the second is giving the error message of "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".

First piece of coding:
xl_WksheetData.Range("A1").Select
Range("A1:P" & CStr(intLastRow)).Sort Key1:=Range("C2"),
Order1:=xlAscending, Key2:=Range _
("E2"), Order2:=xlAscending, Key3:=Range("D2"), Order3:=xlAscending, _
Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:= _
xlTopToBottom, DataOption1:=xlSortNormal, DataOption2:=xlSortNormal, _
DataOption3:=xlSortNormal

2nd piece of coding:
xl_WksheetData.Range("A1").Select
xl_WksheetData.Range("A1:P" & CStr(intLastRow)).Sort Key1:=Range("C2"),
Order1:=xlAscending, Key2:=Range _
("E2"), Order2:=xlAscending, Key3:=Range("D2"), Order3:=xlAscending, _
Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:= _
xlTopToBottom, DataOption1:=xlSortNormal, DataOption2:=xlSortNormal, _
DataOption3:=xlSortNormal
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default Sort error?

The two pieces of code are essentially identical. The only difference is your
reference to the sheet in your sort, which is completely unnecessary. When
the sheet is omitted as in your first piece of code the default value is
activesheet. Since you did a select immediately prior we know that xl
_WksheetData is the active sheet. To that end my guess is that there is more
to this than you have posted. If I were writing that piece of code here is
what I would have...

With xl_WksheetData
.Range(.Range("A1"), .Cells(.Rows.Count, "P").End(xlUp)).Sort _
Key1:=.Range("C2"), Order1:=xlAscending, _
Key2:=.Range("E2"), Order2:=xlAscending, _
Key3:=.Range("D2"), Order3:=xlAscending, _
Header:=xlYes
End With

A few things to note. I got ride of many of the sort arguments as they are
not necessary. I changed the header from xlGuess to xlYes. I got rid of the
intLastRow as you probably don't need it unless you are using it other
places. I also qualified all of the ranges (note the dot in front of the word
range) which means that every range is explicitly from the sheet
xl_WksheetData. That means that I can run this code even if xl_WksheetData is
not the active sheet.
--
HTH...

Jim Thomlinson


"mc" wrote:

I don't know what the difference is between these 2 pieces of code. The
first piece works but the second is giving the error message of "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".

First piece of coding:
xl_WksheetData.Range("A1").Select
Range("A1:P" & CStr(intLastRow)).Sort Key1:=Range("C2"),
Order1:=xlAscending, Key2:=Range _
("E2"), Order2:=xlAscending, Key3:=Range("D2"), Order3:=xlAscending, _
Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:= _
xlTopToBottom, DataOption1:=xlSortNormal, DataOption2:=xlSortNormal, _
DataOption3:=xlSortNormal

2nd piece of coding:
xl_WksheetData.Range("A1").Select
xl_WksheetData.Range("A1:P" & CStr(intLastRow)).Sort Key1:=Range("C2"),
Order1:=xlAscending, Key2:=Range _
("E2"), Order2:=xlAscending, Key3:=Range("D2"), Order3:=xlAscending, _
Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:= _
xlTopToBottom, DataOption1:=xlSortNormal, DataOption2:=xlSortNormal, _
DataOption3:=xlSortNormal

  #3   Report Post  
Posted to microsoft.public.excel.programming
MC MC is offline
external usenet poster
 
Posts: 29
Default Sort error?

Thanks, it worked.

"Jim Thomlinson" wrote:

The two pieces of code are essentially identical. The only difference is your
reference to the sheet in your sort, which is completely unnecessary. When
the sheet is omitted as in your first piece of code the default value is
activesheet. Since you did a select immediately prior we know that xl
_WksheetData is the active sheet. To that end my guess is that there is more
to this than you have posted. If I were writing that piece of code here is
what I would have...

With xl_WksheetData
.Range(.Range("A1"), .Cells(.Rows.Count, "P").End(xlUp)).Sort _
Key1:=.Range("C2"), Order1:=xlAscending, _
Key2:=.Range("E2"), Order2:=xlAscending, _
Key3:=.Range("D2"), Order3:=xlAscending, _
Header:=xlYes
End With

A few things to note. I got ride of many of the sort arguments as they are
not necessary. I changed the header from xlGuess to xlYes. I got rid of the
intLastRow as you probably don't need it unless you are using it other
places. I also qualified all of the ranges (note the dot in front of the word
range) which means that every range is explicitly from the sheet
xl_WksheetData. That means that I can run this code even if xl_WksheetData is
not the active sheet.
--
HTH...

Jim Thomlinson


"mc" wrote:

I don't know what the difference is between these 2 pieces of code. The
first piece works but the second is giving the error message of "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".

First piece of coding:
xl_WksheetData.Range("A1").Select
Range("A1:P" & CStr(intLastRow)).Sort Key1:=Range("C2"),
Order1:=xlAscending, Key2:=Range _
("E2"), Order2:=xlAscending, Key3:=Range("D2"), Order3:=xlAscending, _
Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:= _
xlTopToBottom, DataOption1:=xlSortNormal, DataOption2:=xlSortNormal, _
DataOption3:=xlSortNormal

2nd piece of coding:
xl_WksheetData.Range("A1").Select
xl_WksheetData.Range("A1:P" & CStr(intLastRow)).Sort Key1:=Range("C2"),
Order1:=xlAscending, Key2:=Range _
("E2"), Order2:=xlAscending, Key3:=Range("D2"), Order3:=xlAscending, _
Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:= _
xlTopToBottom, DataOption1:=xlSortNormal, DataOption2:=xlSortNormal, _
DataOption3:=xlSortNormal

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bubble sort Error (Compile Error: Type Mismtach) Excel Monkey[_2_] Excel Programming 6 April 24th 09 12:16 AM
vlookup and sort error LiveUser Excel Worksheet Functions 2 November 7th 07 03:41 PM
Sort error jpas1954 Excel Discussion (Misc queries) 1 January 24th 07 04:24 PM
Sort Error Sprinks Excel Programming 2 December 22nd 05 06:43 PM
Selection.Sort error Krager Excel Programming 3 September 3rd 05 06:12 PM


All times are GMT +1. The time now is 08:58 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"