PDA

View Full Version : Sort not working anymore



Ago
05-22-2014, 04:02 AM
The code crashes when the sort is applied.
Why?
It has worked fine in the past. But now all of the sudden it doesn't work.

Any ideas why?


Sub test()
Application.ScreenUpdating = False
Sheets(1).Activate



With ActiveSheet.QueryTables.Add(Connection:="URL;http://hellis.me/Test/new.php", Destination:=Range("$AH$1"))
.Name = "new"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlAllTables
.WebFormatting = xlWebFormattingNone
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With

ActiveSheet.Sort.SortFields.Clear
ActiveSheet.Sort.SortFields.Add Key:=Range("AH1:AN" & Range("AH" & Rows.Count).End(xlUp).Row), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveSheet.Sort
.SetRange Range("AH1:AN" & Range("AH" & Rows.Count).End(xlUp).Row)
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

End Sub





Get this error message
11722

Bob Phillips
05-22-2014, 06:55 AM
Try fully qualifying


With ActiveSheet.Sort
.SetRange ActiveSheet.Range("AH1:AN" & ActiveSheet.Range("AH" & ActiveSheet.Rows.Count).End(xlUp).Row)
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

p45cal
05-22-2014, 06:56 AM
change:
~Add Key:=Range("AH1:AN" & Range~
to:
~Add Key:=Range("AH1:AH" & Range~
but you don't need that complexity; changing it to:
~Add Key:=Range("AH1"), SortOn:=~
will do.