Ahmed Saad
New Member
I am running this code on excel and donot know why i am getting the run time error "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 stops at .Apply (The part of code is shown below). Kindly help. I need that urgently. The complete code file is also attached.
Code:
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("M2:M3000" _
), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("I1:P3000")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Range("I1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.AutoFilter
ActiveSheet.Range("$I$1:$P$400000").AutoFilter Field:=8, Criteria1:="#N/A"
Range("I1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("Sheet3").Select
Range("B2").Select
ActiveSheet.Paste
Sheets("Sheet1").Select
ActiveSheet.Range("$I$1:$P$400000").AutoFilter Field:=8
Selection.AutoFilter
Range("I1:N1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("Sheet4").Select
Range("B2").Select
ActiveSheet.Paste
Range("B2").Select
Range("B2:G2").Select
Selection.Copy
Range("I2").Select
ActiveSheet.Paste
Range("A2").Select
Application.CutCopyMode = False
Range("B2:G3001").Select
Selection.AutoFilter
ActiveWorkbook.Names.Add Name:="Sheet4!_FilterDatabase", RefersTo:="=Sheet4!$B$2:$G$3001", Visible:=False
Range("B2").Select
ActiveWindow.ScrollRow = 2
Range("B2:G3001").AutoFilter Field:=1, Criteria1:=Array("#N/A"), Operator:=xlFilterValues
ActiveWorkbook.Names.Add Name:="Sheet4!_FilterDatabase", RefersTo:="=Sheet4!$B$2:$G$3001", Visible:=False
Range("B2:G3001").Select
Selection.Delete Shift:=xlShiftUp
ActiveWindow.ScrollRow = 2973
ActiveWindow.ScrollRow = 1
Range("I2:N2").Select
Selection.Cut
Range("B2").Select
ActiveSheet.Paste
Range("B2").Select
End Sub
Attachments
Last edited by a moderator: