LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 173
Default Runtime error 1004 Application-defined or object defined error"on data sort

Chris,

The Range.Sort method is limited to three key fields, hence the error. If you have Excel 2007 or later, try the code called "SortXL2007" below. If you are using 2003, try the other code.

Regards,

Ben

Sub SortXL2007()
Dim lFirstRow As Long
Dim lLastRow As Long
Dim vCells() As String

vCells() = Split(Range("Data").Address, ":")
lFirstRow = Range(vCells(0)).Row
lLastRow = Range(vCells(1)).Row

With ActiveWorkbook.Worksheets("Sheet1").sort
.SortFields.Clear
.SortFields.Add _
Key:=Range("$P$" & lFirstRow & ":$P$" & lLastRow), _
SortOn:=xlSortOnValues, Order:=xlDescending _
, DataOption:=xlSortNormal
.SortFields.Add _
Key:=Range("$B$" & lFirstRow & ":$B$" & lLastRow), _
SortOn:=xlSortOnValues, Order:=xlAscending _
, DataOption:=xlSortNormal
.SortFields.Add _
Key:=Range("$M$" & lFirstRow & ":$M$" & lLastRow), _
SortOn:=xlSortOnValues, Order:=xlAscending _
, DataOption:=xlSortNormal
.SortFields.Add _
Key:=Range("$K$" & lFirstRow & ":$K$" & lLastRow), _
SortOn:=xlSortOnValues, Order:= _
xlAscending, DataOption:=xlSortNormal
.SetRange Range("Data")
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

End Sub

Sub SortXL2003()

With Range("Data")
.sort Key1:=Range("K12"), Order1:=xlAscending, Header:=xlNo _
, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal

.sort Key1:=Range("P12"), Order1:=xlDescending, _
Key2:=Range("B12"), Order2:=xlAscending, Key3:=Range("M12"), _
Order3:=xlAscending, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
End With

End Sub
 
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
Runtime error 1004- application defined or object defined error Novice Excel Programming 11 February 6th 06 11:02 PM
Runtime error 1004- application defined or object defined erro Novice Excel Programming 0 February 6th 06 09:34 PM
Runtime error 1004- application defined or object defined error Jim Thomlinson[_5_] Excel Programming 0 February 6th 06 09:33 PM
Runtime error 1004- application defined or object defined erro Novice Excel Programming 1 February 6th 06 09:33 PM
Runtime error 1004- application defined or object defined erro Jim Thomlinson[_5_] Excel Programming 0 February 6th 06 09:29 PM


All times are GMT +1. The time now is 09:55 AM.

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

About Us

"It's about Microsoft Excel"