The code below sort by data on a set range
lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
lastcol = .Cells(1, Columns.Count).End(xlToLeft).Column
mySheet.Sort.SortFields.Clear
mySheet.Sort.SortFields.Add Key:=Range("A2:A" & lastrow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With mySheet.Sort
.SetRange Range("A1:U" & lastrow)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
The number of columns can vary each time the code is executed. Is there a way to adapt this code so that the range uses both lastrow and lastcol?
Yes, see below adjustment (not tested), but you should get the logic.
With mySheet.Sort
.SetRange Range(mySheet.Cells(1,1), mySheet.Cells(lastrow,lastcol))
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Related
I recoded a macro and then have been trying to edit the code so that it will sort the chart no matter how many values it has. I am getting an error about range of object global failed.
LastRow = Cells(Rows.Count, 17).End(xlUp).Row
LastCol = Cells(7, Columns.Count).End(xlToLeft).Column
ActiveWorkbook.Worksheets("Data").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Data").Sort.SortFields.Add2 Key:=Range("Q7", LastRow), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Data").Sort
.SetRange Range("Q7", LastCol)
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
ActiveWorkbook.Worksheets("Data").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Data").Sort.SortFields.Add2 Key:=Range("Q7", Cells(LastRow, "Q")), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Data").Sort
.SetRange Range("Q7", Cells(LastRow, LastCol))
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
I want to sort column A without using select but I keep Getting error massage,
Unable to get the sort property of the range class
'Sort Managers
With QueWS.Columns("A:A")
.Sort.SortFields.Clear
.Sort.SortFields.Add Key:=Range("A2:A" & LastRow), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Que").Sort
.SetRange Range("A1", Cells(LastRow, LastColumn))
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End With
Try this
With ActiveWorkbook.Worksheets("Que")
.Sort.SortFields.Clear
.Sort.SortFields.Add Key:=Range("A1"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Que").Sort
.SetRange Range("A2:A" & LastRow)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End With
I am trying to sort column A in descending order with unknown number of cells.
Getting run-time error - Method 'Sort' of object'_Worksheet' failed.
This code was working until recently when I got a new laptop with a different version of Excel.
lastrow = Cells(Rows.Count, 1).End(xlUp).Row
Range("A1").Select
ActiveWorkbook.Worksheets("New Working").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("New Working").Sort.SortFields.Add Key:=Range("A1") _
, SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("New Working").Sort
.SetRange Range("A2:AT" & lastrow)
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
The issue might come from the fact that you aren't fully qualifying your Worksheet when getting the value for the Last Row, so Excel would be looking at the ActiveSheet rather than the one you are wanting, I would suggest using something like the code below, where it is fully qualified:
Sub foo()
Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("New Working")
lastrow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
ws.Sort.SortFields.Clear
ws.Sort.SortFields.Add Key:=ws.Range("A1"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
With ws.Sort
.SetRange ws.Range("A1:AT" & lastrow)
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
I am in Excel I have collected a variable correctly.
Dim mysheet As String
mysheet = ActiveSheet.Name
mysheet has a value os in one case "Agent_State_Log20180320" - so this part seems right.
I am trying to use it in the following places - but my syntax is off.
ActiveWorkbook.Worksheets
(mysheet).AutoFilter.Sort.SortFields.ClearActiveWorkbook.Worksheets
(mysheet).AutoFilter.Sort.SortFields.Add Key:=Range
("C1"),SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= xlSortNormal
With ActiveWorkbook.Worksheets(mysheet).AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
What should I change to get this to work correctly?
Try this
ActiveSheet.AutoFilter.Sort.SortFields.Clear
ActiveSheet.AutoFilter.Sort.SortFields.Add Key:=Range("C1"),SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= xlSortNormal
With ActiveSheet.AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
The code is from slightly edited macro. I tried to remove the 'messy code', however it is not working. The aim of it is to sort data in column BF from A to Z.
Dim InSheet As Worksheet
Set InSheet = ThisWorkbook.Worksheets("A to Z")
Dim LastRow as Integer
LastRow = InSheet.Cells(Rows.Count, 58).End(xlUp).Row
InSheet.Select
Columns("BF:BF").Select
InSheet.Sort.SortFields.Clear
InSheet.Sort.SortFields.Add Key:=Range( _
"BF1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With InSheet.Sort
.SetRange Range("A1:BF" & LastRow)
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
SO I tried this and it is not working:
Dim InSheet As Worksheet
Set InSheet = ThisWorkbook.Worksheets("A to Z")
Dim LastRow as Integer
LastRow = InSheet.Cells(Rows.Count, 58).End(xlUp).Row
InSheet.Columns("BF:BF")
InSheet.Sort.SortFields.Clear
InSheet.Sort.SortFields.Add Key:=Range( _
"BF1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With InSheet.Sort
.SetRange Range("A1:BF" & LastRow)
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Any ideas? I would like to really avoid .select an other stuff as it is hugely decreasing performance.
You can just directly sort the Range with a Key with out the rest of the code you have.
Range("A1:BF" & LastRow).SORT Key1:=Range("BF1"), Order1:=xlAscending_
Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:= _
xlTopToBottom, DataOption1:=xlSortNormal
I used advice provided by both Steven Martin's answer and Dmitry Pavliv's comment, and this is working pretty well:
Dim InSheet As Worksheet
Set InSheet = ThisWorkbook.Worksheets("A to Z")
Dim LastRow As Integer
LastRow = InSheet.Cells(Rows.Count, 58).End(xlUp).Row
With InSheet.Sort ' sort data from A to Z
.SetRange InSheet.Range("A1:BF" & LastRow)
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With