Excel VBA Run-Time Error 1004 - excel

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

Related

'1004': That 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

I am trying to sort a range for one value to display in order from smallest to largest, but for some reason am getting the above error at ".Apply". Does anyone know how to fix this? this is for something due soon, so prompt help would be a real lifesaver!
Selection.AutoFilter
ActiveWorkbook.Worksheets("Filter ClientID").AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Filter ClientID").AutoFilter.Sort.SortFields.Add2 _
Key:=Range("O9:O50"), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Filter ClientID").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

VBA To Filter Data

I have a macro which was running for the past number of weeks. The macro started failing. I added some with end withs to the code and solved the initial problem.
My code is now producing a "Syntax Error". Is there something I need to change in the with ?
Code
Sheets("Graph Worksheet").Select
Rows("1:1").Select
Selection.AutoFilter
With ActiveWorkbook.Worksheets("Graph Worksheet").AutoFilter.Sort.SortFields.Clear
End With
With ActiveWorkbook.Worksheets("Graph Worksheet").AutoFilter.Sort.SortFields.Add2 Key:= _
Range("D1"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
xlSortNormal End With
With ActiveWorkbook.Worksheets("Graph Worksheet").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Taken from Microsoft for With.. End With:
Executes a series of statements that repeatedly refer to a single object or structure so that the statements can use a simplified syntax when accessing members of the object or structure. When using a structure, you can only read the values of members or invoke methods, and you get an error if you try to assign values to members of a structure used in a With...End With statement.
Also:
objectExpression Required. An expression that evaluates to an object. The expression may be arbitrarily complex and is evaluated only once. The expression can evaluate to any data type, including elementary types.
In your example, the first 2 With.. End With are not needed at all, and can be omitted.
You also do not need to use .Select and should refrain from using this.
See below code:
Sub helper()
Sheets("Graph Worksheet").Rows("1:1").AutoFilter
ActiveWorkbook.Worksheets("Graph Worksheet").AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Graph Worksheet").AutoFilter.Sort.SortFields.Add2 Key:=Range("D1"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Graph Worksheet").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
As you will see the above compiles, but I have not tested it's use.
Solved using below code
Sheets("Graph Worksheet").Rows("1:1").AutoFilter
ActiveWorkbook.Worksheets("Graph Worksheet").AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Graph Worksheet").AutoFilter.Sort.SortFields.Add _
Key:=Range("D1"), SortOn:=xlSortOnValues, Order:=xlDescending, _
DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Graph Worksheet").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

Auto sorting and filtering macro worked in Excel 2010 but no longer in Excel 2016

I have a macro that filters and sorts service desk tickets into different color coded categories and severity levels. It used to work in Excel 2010 but I'm trying to run it in Excel 2016 and it's throwing 2 errors.
The first is
"Run-time error 91 - Object variable or With block variable not set"
It seems to be stuck on this line:
ActiveWorkbook.Worksheets(SheetName1).AutoFilter.Sort.SortFields.Clear
and the second error is
"Run-time error '1004' - 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"
That error is pointing to .Apply in the code below.
I'm not familiar with VBA code at all, so I'm quite stuck, I've inherited the code from a colleague. The code snippet throwing the errors are below.
Range("A1:S1").Select
Rows("1:1").Select
Selection.AutoFilter
Range("V3").Select
ActiveWorkbook.Worksheets(SheetName1).AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets(SheetName1).AutoFilter.Sort.SortFields.Add Key:= _
Range("V1"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets(SheetName1).AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Added the code that quantifies variable SheetName1
'Insert Active.Worksheetname to A300. This is required to allow the filters to function later on, as the worksheet name varies according to the exported CSV file name
ActiveWindow.SmallScroll Down:=288
Range("A300").Select
ActiveCell.FormulaR1C1 = _
"=MID(CELL(""filename"",R[-299]C),FIND(""]"",CELL(""filename"",R[-299]C),1)+1,255)"
Dim SheetName1
SheetName1 = Trim(Range("A300").Value)
Any help to get this code working would be much appreciated.
I've now edited that section to be as follows:
Range("A1:S1").Select
Rows("1:1").Select
Selection.AutoFilter
Range("V3").Select
With ThisWorkbook.Sheets(SheetName1)
.AutoFilterMode = False
.Range("A1").AutoFilter
.AutoFilter.Sort.SortFields.Clear
.AutoFilter.Sort.SortFields.Add Key:=.Range("V1"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
With .AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End With
Error 1004 still being thrown and highlighted ".Apply"
I'm really not a programmer or understand code at all really, so excuse the lack of comprehension of your answers.
Here is a basic code example, use With statements to properly qualify your sheet, remove and don't use Select, comments are provided to clarify.
With ThisWorkbook.Sheets(SheetName1) 'If `SHeetName1` is the actual sheet name it must be in parentheses ""
.AutoFilterMode = False 'Remove filters
.Range("A1").AutoFilter
.AutoFilter.Sort.SortFields.Clear 'Will fail if the previous filter was not removed
.AutoFilter.Sort.SortFields.Add Key:=.Range("V1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With .AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End With

VBA sorting table in other workbook

Good morning,
I have a macro that, among other things, sorts a table in another workbook. Here's the relevant bits of code
Set wbLSHP = Workbooks("CDU_Enrollee Engagement Tracking Report v2.2.xlsx")
Set wsLSHP = wbLSHP.Worksheets("Sheet1")
"wsLSHP" is the worksheet where the table is located. The table is named "Table1"
wsLSHP.ListObjects("Table1").Sort.SortFields.Clear
wsLSHP.ListObjects("Table1").Sort.SortFields.add _
Key:=Range("Table1[[#All],[CHW First Name]]"), SortOn:=xlSortOnValues, _
Order:=xlAscending, DataOption:=xlSortNormal
With wsLSHP.ListObjects("Table1").Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
When running this, I get the following error:
"Run-time error '1004':, Application-defined or object-defined error
This happens at the following line:
wsLSHP.ListObjects("Table1").Sort.SortFields.add _
Key:=Range("Table1[[#All],[CHW First Name]]"), SortOn:=xlSortOnValues, _
Order:=xlAscending, DataOption:=xlSortNormal
Any ideas on what's causing this error?

Excel Active Sheet | Active Table | Sort

I am looking to sort An Active Worksheet/Active table. I will be using an excel file that will be growing daily and each day will have a new sheet/table. I am looking to have everything automatically setup for a different employee to do the work.
I found using ActiveSheet.ListObjects(1) will choose the active table, but sorting is causing issues.
If i use the following format it would be a one time sort, that in the end would be useless for my employee.
Sub Test()
ActiveWorkbook.Worksheets("Sheet1").ListObjects("Table1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").ListObjects("Table1").Sort.SortFields.Add _
Key:=Range("Table1[[#All],[Client Name]]"), SortOn:=xlSortOnValues, Order _
:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").ListObjects("Table1").Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
I tried the following but it didn't work.
Sub Test()
ActiveSheet.ListObjects(1).Sort.SortFields.Clear
ActiveSheet.ListObjects(1).Sort.SortFields.Add _
Key:=Range("ActiveSheet.ListObjects(1)[[#All],[Client Name]]"), SortOn:=xlSortOnValues, Order _
:=xlAscending, DataOption:=xlSortNormal
With ActiveSheet.ListObjects(1).Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
All codes go back to the label of the table. How can I modify this to just use what is active?
I believe you the following code will do what you expect, the main different is in the Key, I used the name property of the ListObject to get the correct name in there:
Sub Test()
ActiveSheet.ListObjects(1).Sort.SortFields.Clear
ActiveSheet.ListObjects(1).Sort.SortFields.Add _
Key:=Range(ActiveSheet.ListObjects(1).Name & "[[#All],[Client Name]]"), SortOn:=xlSortOnValues, Order _
:=xlAscending, DataOption:=xlSortNormal
With ActiveSheet.ListObjects(1).Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub

Resources