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 have this macro that works on my office computer but when I run it at a client's computer It doesn't work and shows the
Error 438 object doesn't support this property
marking this line:
ActiveWorkbook.Worksheets("Hoja1").Sort.SortFields.Add2 Key:=Range("E2:E" & nFilas)
(My excel version is 365 and the client's is 2013)
I've tried changing Worksheets by Sheets, ActiveWorkbook by Sheet but nothing worked.
Columns("E:G").Select
ActiveSheet.Range("$E$1:$G$" & nFilas).RemoveDuplicates Columns:=Array(1, 2, 3), _
Header:=xlYes
Cells.Select
ActiveWorkbook.Worksheets("Hoja1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Hoja1").Sort.SortFields.Add2 Key:=Range("E2:E" & nFilas) _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Hoja1").Sort
.SetRange Range("A1:X" & nFilas)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Change .Add2 into .Add I think .Add2 was introduced in a later Excel version.
I recommend the following improvement:
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Hoja1")
ws.Range("$E$1:$G$" & nFilas).RemoveDuplicates Columns:=Array(1, 2, 3), Header:=xlYes
With ws.Sort
.SortFields.Clear
.SortFields.Add Key:=ws.Range("E2:E" & nFilas), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SetRange ws.Range("A1:X" & nFilas)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
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
I am extremely new at working with macros in Excel. I used the 'Record Macro' feature in Excel 2016 to build a filter and sort macro for a single column and then iterated in the code editor. I'm not sure what's wrong with it but it keeps crashing my program. The code is below. Any help at all would be great.
Sub Filter()
'
' Filter Macro
' Filter workbook
'
' Keyboard Shortcut: Ctrl+Shift+A
'
ActiveWorkbook.Worksheets("Uncategorized Products").AutoFilter.Sort.SortFields. _
Clear
ActiveWorkbook.Worksheets("Uncategorized Products").AutoFilter.Sort.SortFields. _
Add Key:=Range("B1:B62637"), SortOn:=xlSortOnValues, Order:=xlDescending _
, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Uncategorized Products").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Sheets("Uncategorized Products").Select
ActiveSheet.Range("$A$1:$I$62637").AutoFilter Field:=6, Criteria1:="=0"
ActiveSheet.Range("$A$1:$I$62637").AutoFilter Field:=2, Criteria1:="<>"""
ActiveWorkbook.Worksheets("Missing Images").AutoFilter.Sort.SortFields. _
Clear
ActiveWorkbook.Worksheets("Missing Images").AutoFilter.Sort.SortFields. _
Add Key:=Range("B1:B62637"), SortOn:=xlSortOnValues, Order:=xlDescending _
, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Missing Images").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Sheets("Missing Images").Select
ActiveSheet.Range("$A$1:$I$62637").AutoFilter Field:=5, Criteria1:="=FALSE"
ActiveSheet.Range("$A$1:$I$62637").AutoFilter Field:=1, Criteria1:="<>"""
ActiveWorkbook.Worksheets("RWI Settings").AutoFilter.Sort.SortFields. _
Clear
ActiveWorkbook.Worksheets("RWI Settings").AutoFilter.Sort.SortFields. _
Add Key:=Range("B1:B62637"), SortOn:=xlSortOnValues, Order:=xlDescending _
, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("RWI Settings").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Sheets("RWI Settings").Select
ActiveSheet.Range("$A$1:$I$62637").AutoFilter Field:=2, Criteria1:="<>"""
ActiveWorkbook.Worksheets("Names & Descriptions").AutoFilter.Sort.SortFields. _
Clear
ActiveWorkbook.Worksheets("Names & Descriptions").AutoFilter.Sort.SortFields. _
Add Key:=Range("A1:A62637"), SortOn:=xlSortOnValues, Order:=xlDescending _
, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Names & Descriptions").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Sheets("Names & Descriptions").Select
ActiveSheet.Range("$A$1:$I$62637").AutoFilter Field:=5, Criteria1:="=TRUE"
ActiveSheet.Range("$A$1:$I$62637").AutoFilter Field:=1, Criteria1:="<>"""
ActiveWorkbook.Worksheets("Disabled Items").AutoFilter.Sort.SortFields. _
Clear
ActiveWorkbook.Worksheets("Disabled Items").AutoFilter.Sort.SortFields. _
Add Key:=Range("A1:A62637"), SortOn:=xlSortOnValues, Order:=xlDescending _
, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Disabled Items").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Sheets("Disabled Items").Select
ActiveSheet.Range("$A$1:$I$62637").AutoFilter Field:=1, Criteria1:="<>"""
ActiveWorkbook.Worksheets("Boost Scores").AutoFilter.Sort.SortFields. _
Clear
ActiveWorkbook.Worksheets("Boost Scores").AutoFilter.Sort.SortFields. _
Add Key:=Range("A1:A62637"), SortOn:=xlSortOnValues, Order:=xlDescending _
, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Boost Scores").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Sheets("Boost Scores").Select
ActiveSheet.Range("$A$1:$I$62637").AutoFilter Field:=1, Criteria1:="<>"""
End Sub
Fixed my issue. New code that works pasted below:
Sub sort()
'
' sort Macro
'
' Keyboard Shortcut: Ctrl+Shift+A
'
Dim lastRow As Long
Sheets("Store Product Data").Activate
'Search for any entry, by searching backwards by Rows.
lastRow = Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
With Sheets("Uncategorized Products")
With .Range("A2:F" & lastRow)
.Formula = "=iferror(INDEX('Store Product Data'!$A:$R,'Key'!$A1+1,MATCH(A$1,'Store Product Data'!$A$1:$R$1,0)),0)"
.Value = .Value
End With
End With
With Sheets("Missing Images")
With .Range("A2:E" & lastRow)
.Formula = "=iferror(INDEX('Store Product Data'!$A:$R,'Key'!$A1+1,MATCH(A$1,'Store Product Data'!$A$1:$R$1,0)),0)"
.Value = .Value
End With
End With
With Sheets("RWI Settings")
With .Range("A2:H" & lastRow)
.Formula = "=iferror(INDEX('Store Product Data'!$A:$R,'Key'!$A1+1,MATCH(A$1,'Store Product Data'!$A$1:$R$1,0)),0)"
.Value = .Value
End With
End With
With Sheets("Names & Descriptions")
With .Range("A2:E" & lastRow)
.Formula = "=iferror(INDEX('Store Product Data'!$A:$R,'Key'!$A1+1,MATCH(A$1,'Store Product Data'!$A$1:$R$1,0)),0)"
.Value = .Value
End With
End With
With Sheets("Disabled Items")
With .Range("A2:D" & lastRow)
.Formula = "=iferror(INDEX('Store Product Data'!$A:$R,'Key'!$A1+1,MATCH(A$1,'Store Product Data'!$A$1:$R$1,0)),0)"
.Value = .Value
End With
End With
With Sheets("Boost Scores")
With .Range("A2:E" & lastRow)
.Formula = "=iferror(INDEX('Store Product Data'!$A:$R,'Key'!$A1+1,MATCH(A$1,'Store Product Data'!$A$1:$R$1,0)),0)"
.Value = .Value
End With
End With
Sheets("Uncategorized Products").Select
ActiveWorkbook.Worksheets("Uncategorized Products").AutoFilter.sort.SortFields. _
Clear
ActiveWorkbook.Worksheets("Uncategorized Products").AutoFilter.sort.SortFields. _
Add Key:=Range("B1:B50000"), SortOn:=xlSortOnValues, Order:=xlDescending _
, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Uncategorized Products").AutoFilter.sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
ActiveWorkbook.Worksheets("Uncategorized Products").AutoFilter.sort.SortFields. _
Clear
ActiveWorkbook.Worksheets("Uncategorized Products").AutoFilter.sort.SortFields. _
Add Key:=Range("E1:E50000"), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Uncategorized Products").AutoFilter.sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
ActiveWorkbook.Worksheets("Uncategorized Products").AutoFilter.sort.SortFields. _
Clear
ActiveWorkbook.Worksheets("Uncategorized Products").AutoFilter.sort.SortFields. _
Add Key:=Range("F1:F50000"), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Uncategorized Products").AutoFilter.sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Sheets("Missing Images").Select
ActiveWorkbook.Worksheets("Missing Images").AutoFilter.sort.SortFields. _
Clear
ActiveWorkbook.Worksheets("Missing Images").AutoFilter.sort.SortFields. _
Add Key:=Range("A1:A50000"), SortOn:=xlSortOnValues, Order:=xlDescending _
, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Missing Images").AutoFilter.sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
ActiveWorkbook.Worksheets("Missing Images").AutoFilter.sort.SortFields. _
Clear
ActiveWorkbook.Worksheets("Missing Images").AutoFilter.sort.SortFields. _
Add Key:=Range("D1:D50000"), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Missing Images").AutoFilter.sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
ActiveWorkbook.Worksheets("Missing Images").AutoFilter.sort.SortFields. _
Clear
ActiveWorkbook.Worksheets("Missing Images").AutoFilter.sort.SortFields. _
Add Key:=Range("E1:E50000"), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Missing Images").AutoFilter.sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Sheets("RWI Settings").Select
ActiveWorkbook.Worksheets("RWI Settings").AutoFilter.sort.SortFields. _
Clear
ActiveWorkbook.Worksheets("RWI Settings").AutoFilter.sort.SortFields. _
Add Key:=Range("B1:B50000"), SortOn:=xlSortOnValues, Order:=xlDescending _
, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("RWI Settings").AutoFilter.sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
ActiveWorkbook.Worksheets("RWI Settings").AutoFilter.sort.SortFields. _
Clear
ActiveWorkbook.Worksheets("RWI Settings").AutoFilter.sort.SortFields. _
Add Key:=Range("A1:A50000"), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("RWI Settings").AutoFilter.sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Sheets("Names & Descriptions").Select
ActiveWorkbook.Worksheets("Names & Descriptions").AutoFilter.sort.SortFields. _
Clear
ActiveWorkbook.Worksheets("Names & Descriptions").AutoFilter.sort.SortFields. _
Add Key:=Range("A1:A50000"), SortOn:=xlSortOnValues, Order:=xlDescending _
, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Names & Descriptions").AutoFilter.sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
ActiveWorkbook.Worksheets("Names & Descriptions").AutoFilter.sort.SortFields. _
Clear
ActiveWorkbook.Worksheets("Names & Descriptions").AutoFilter.sort.SortFields. _
Add Key:=Range("B1:B50000"), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Names & Descriptions").AutoFilter.sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
ActiveWorkbook.Worksheets("Names & Descriptions").AutoFilter.sort.SortFields. _
Clear
ActiveWorkbook.Worksheets("Names & Descriptions").AutoFilter.sort.SortFields. _
Add Key:=Range("e1:e50000"), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Names & Descriptions").AutoFilter.sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Sheets("Disabled Items").Select
ActiveWorkbook.Worksheets("Disabled Items").AutoFilter.sort.SortFields. _
Clear
ActiveWorkbook.Worksheets("Disabled Items").AutoFilter.sort.SortFields. _
Add Key:=Range("A1:A50000"), SortOn:=xlSortOnValues, Order:=xlDescending _
, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Disabled Items").AutoFilter.sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
ActiveWorkbook.Worksheets("Disabled Items").AutoFilter.sort.SortFields. _
Clear
ActiveWorkbook.Worksheets("Disabled Items").AutoFilter.sort.SortFields. _
Add Key:=Range("D1:D50000"), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Disabled Items").AutoFilter.sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Sheets("Boost Scores").Select
ActiveWorkbook.Worksheets("Boost Scores").AutoFilter.sort.SortFields. _
Clear
ActiveWorkbook.Worksheets("Boost Scores").AutoFilter.sort.SortFields. _
Add Key:=Range("A1:A50000"), SortOn:=xlSortOnValues, Order:=xlDescending _
, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Boost Scores").AutoFilter.sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
ActiveWorkbook.Worksheets("Boost Scores").AutoFilter.sort.SortFields. _
Clear
ActiveWorkbook.Worksheets("Boost Scores").AutoFilter.sort.SortFields. _
Add Key:=Range("D1:D50000"), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Boost Scores").AutoFilter.sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
hoping this newbie here could get some assistance with sorting using macro please.
I have a table ranging D:R to be sorted.
Firstly, the whole table needs to be sorted in ascending order according to column Q.
Then the same table get sorted in ascending order according to column P.
Lastly the whole table needs to bring yellow highlighted rows per column D to the top.
My current formula is recorded using the in-built recorder and it doesn't work.
Would anyone provide any suggestions? Many thanks in advance!
'Sorting for easier read
'Check for filter, turn on if none exists
If Not ActiveSheet.AutoFilterMode Then
ActiveSheet.Range("D1:R1").AutoFilter
End If
'Sort in order of column Q
ActiveWorkbook.Worksheets(4).AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets(4).AutoFilter.Sort.SortFields.Add Key:=Range( _
"Q1:Q10000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets(4).AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
End With
'Sort in order of column P
ActiveWorkbook.Worksheets(4).AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets(4).AutoFilter.Sort.SortFields.Add Key:=Range( _
"P1:P10000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets(4).AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
End With
'Bring highlighted row to the top
ActiveWorkbook.Worksheets(4).AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets(4).AutoFilter.Sort.SortFields.Add(Range("D:D" _
), xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(255 _
, 255, 0)
With ActiveWorkbook.Worksheets(4).AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
End With
Here's one that I did to sort column 52 on color yellow:
Sub SortOnDuplicates()
ws.ListObjects("Table1").Range.AutoFilter _
Field:=52, _
Criteria1:=RGB(255, 255, 0), _
Operator:=xlFilterCellColor
End Sub
Here's one for sorting on ascending:
ws.ListObjects("Table1").Range.AutoFilter Field:=52
With ws.ListObjects("Table1").Sort
'Last Name
.SortFields.Clear
.SortFields.Add _
Key:=Range("Table1[[#All],[Last Name]]"), _
SortOn:=xlSortOnValues, _
Order:=xlAscending, _
DataOption:=xlSortNormal
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
'First Name
.SortFields.Clear
.SortFields.Add _
Key:=Range("Table1[[#All],[First Name]]"), _
SortOn:=xlSortOnValues, _
Order:=xlAscending, _
DataOption:=xlSortNormal
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With