I'd want to sort a spreadsheet of data by the oldest "date" in column # 8, as well as by the color "Red" in column 1. I'm new to VBA and would greatly appreciate your assistance. I recorded the code below with a macro recorder, and it only works in newest Office (not 2010).
Sub SortData()
' Sort the PDR data
Application.ScreenUpdating = False
Sheets("data").Select
ActiveWorkbook.Worksheets("data").AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets("data").AutoFilter.Sort.SortFields.Add2 Key:=Range( _
"H2:H2848"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("data").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
ActiveWorkbook.Worksheets("data").AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets("data").AutoFilter.Sort.SortFields.Add(Range( _
"A2:A2848"), xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color _
= RGB(255, 0, 0)
With ActiveWorkbook.Worksheets("data").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Application.ScreenUpdating = True
End Sub
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 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
I am using VBA for Excel 2010 and randomly receiving the following error:
Run-time error '1004': "The sort reference is not valid. Make sure it's within the data you want to sort, and the first Sort By box isn't the same or blank."
This is the code
'Sort the active rows
With ActiveWorkbook.Worksheets("Product Backlog").Sort
.SetRange Range("A4:F51")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
The sort by box is blank, that is your problem. I never have used a Sort object like your doing, but I can see that you have not defined a key, or a range to sort by, just the range to be sorted. A key should be defined such as Range("A4") or something. I looked it up, it should have .sortfields.add (range) in it, such as:
'Sort the active rows
With ActiveWorkbook.Worksheets("Product Backlog").Sort
.SetRange Range("A4:F51")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.SortFields.Add Key:=Range("A4:F51").Columns(1), SortOn:=xlSortOnValues, _
Order:=xlDescending, DataOption:=xlSortNormal
.Apply
End With
I use the Sort function as follows:
ActiveWorkbook.Worksheets("Product Backlog").Range("A4:F51").Sort _
Key1:= ActiveWorkbook.Worksheets("Product Backlog").Range("A4:F51").Columns(1), _
Header:= xlYes, _
Orientation:=xlSortColumns, _
MatchCase:=False, _
SortMethod:=xlPinYin