VBA sorting table in other workbook - excel

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?

Related

Method 'Range' of object '_Worksheet' failed when trying to sort table

I've trying to look for an answer on my error in Excel, what I try to do is to filter the contents in a column on a table from largest to smallest. I recorded a Macro doing this but whenever I put this code into a button I get an "Method 'Range' of object '_Worksheet' failed" after I try to use it.
ThisWorkbook.Sheets("FDTHC").ListObjects("FilterDTHC").Sort.SortFields. _
Clear
ThisWorkbook.Sheets("FDTHC").ListObjects("FilterDTHC").Sort.SortFields. _
Add Key:=Range("Time"), SortOn:=xlSortOnValues, Order _
:=xlDescending, DataOption:=xlSortNormal
With ThisWorkbook.Sheets("FDTHC").ListObjects("FilterDTHC").Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
The VBA highlights the following part of the code:
ThisWorkbook.Sheets("FDTHC").ListObjects("FilterDTHC").Sort.SortFields. _
Add Key:=Range("Time"), SortOn:=xlSortOnValues, Order _
:=xlDescending, DataOption:=xlSortNormal
I appreciate any help given, thanks in advance.
The range set in the command is on the ActiveSheet. It should be linked to Sheet("FDTHC").
ThisWorkbook.Sheets("FDTHC").ListObjects("FilterDTHC").Sort.SortFields.Add _
Key:=ThisWorkbook.Sheets("FDTHC").Range("Time"), _
SortOn:=xlSortOnValues, _
Order:=xlDescending, _
DataOption:=xlSortNormal

Sort recorded in 2016 generates "Error 438. Object doesn't support this property or method" in 2010

I recorded a macro for a 2 column sort, that works, on Excel 2016.
I get the following error on Excel 2010.
Error 438. Object doesn't support this property or method.
What is not acceptable for the 2010 version of Excel?
Sub SortOpenOrder()
'
' Macro1 Macro
'
'
Range("Table2[[#Headers],[Customer No.]]").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
ActiveWorkbook.Worksheets("Open Orders").ListObjects("Table2").Sort.SortFields. _
Clear
ActiveWorkbook.Worksheets("Open Orders").ListObjects("Table2").Sort.SortFields. _
Add2 Key:=Range("Table2[Customer No.]"), SortOn:=xlSortOnValues, Order:= _
xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Open Orders").ListObjects("Table2").Sort.SortFields. _
Add2 Key:=Range("Table2[PO '#]"), SortOn:=xlSortOnValues, Order:= _
xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Open Orders").ListObjects("Table2").Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Range("Table2[[#Headers],[Customer No.]]").Select
End Sub
The code you have doesn't look like it would bomb on either version of Excel, but refactoring it for better coding practices will surely help (as fabio alluded to).
Try this:
Dim myTable as ListObject
Set myTable = ThisWorkbook.Worksheets("Open Orders").ListObjects("Table2")
With myTable.Sort
With .SortFields
.Clear
.Add Key:=myTable.ListColumns("Customer No.").DataBodyRange, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.Add Key:=myTable.ListColumns("PO '#").DataBodyRange, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
End With
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

Sort Error 1004 Method 'Range' of object '_Global' failed

I have a worksheet with a database connection and auto-filter on it I'm trying to sort, and I can't for the life of me figure out why this is not working:
Sub TEST()
Workbooks("1.Receiving Worksheet Database 02 No Filter.xlsx").Activate
Worksheets("Sheet1").Activate
Worksheets("Sheet1").Range("A:AJ").Sort key1:=Range("B"), _
order1:=xlAscending, Header:=xlYes
End Sub
Ok, after reading another post that suggested someone record the macro I was like "duh!", the code that works is below and I think the issues may have had to do with the database connection and the auto-filter.
ActiveWorkbook.Worksheets("Sheet1").ListObjects("Table_Query_from_Prototype"). _
Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").ListObjects("Table_Query_from_Prototype"). _
Sort.SortFields.Add Key:=Range("Table_Query_from_Prototype[[#All],[ID]]"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortTextAsNumbers
With ActiveWorkbook.Worksheets("Sheet1").ListObjects( _
"Table_Query_from_Prototype").Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Nice and simple :|

How to sort filtered data in vba?

I have a worksheet populated with data. I need to filter the data that could only show the info w/in 5 miles. Once the data is filtered to w/in 5 miles, I need to sort the variance column in ascending order. I used a record macro and attempted to incorporate it. The filtering works fine but I get an error saying:
Compile Error: Expected Array
when I run it. Here is a snippet of my code. When the error pops up, the Range is highlighted on the 6th line of code.
.Worksheets("Market Work").Cells.Select
Selection.AutoFilter
.Worksheets("Market Work").Range("$A$1:$Q$" & RowLast2).AutoFilter Field:=5, Criteria1:= _
"w/in 5 miles"
ActiveWorkbook.Worksheets("Market Work").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Market Work").Sort.SortFields.Add Key:=Range( _
"G2:G112"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("Market Work").Sort
.SetRange Range("A1:Q112")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Following code will help you...
Sheets("Finnet").Select
Sheets("Finnet").AutoFilterMode = False
Sheets("Finnet").Range("A1", Range("XFD1").End(xlToLeft)).Select
Sheets("Finnet").Range(Selection, Range("A" & Rows.Count).End(xlUp)).AutoFilter
Sheets("Finnet").AutoFilter.Sort.SortFields.Add Key:=Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row), SortOn:=xlSortOnValues, Order:=xlAscending
Sheets("Finnet").AutoFilter.Sort.Header = xlYes
Sheets("Finnet").AutoFilter.Sort.Apply
Sheets("Finnet").AutoFilterMode = False

Excel VBA Run-Time Error 1004

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

Resources