I have a VBA which works fine (Takes time to execute) in my excel 365 but does not work in excel 2016
I am importing excel as SHEET (2) and sorting first and second sheet respective columns and copying second sheet to first after last used column.
ActiveWorkbook.Sheets("SHEET").Select
Range("C1").Select
Application.CutCopyMode = False
Selection.AutoFilter
ActiveWorkbook.Worksheets("SHEET").AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets("SHEET").AutoFilter.Sort.SortFields.Add2 Key:=Range _
("C1:C50000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("SHEET").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Sheets("SHEET (2)").Select
Range("A1").Select
Selection.AutoFilter
ActiveWorkbook.Worksheets("SHEET (2)").AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets("SHEET (2)").AutoFilter.Sort.SortFields.Add2 Key:= _
Range("A1:A50000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
:=xlSortNormal
With ActiveWorkbook.Worksheets("SHEET (2)").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
ActiveWorkbook.Sheets("SHEET").Select
Sheets("SHEET (2)").Select ' origin sheet
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
X = Selection.Columns.count ' get number of columns
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("SHEET").Select ' destination sheet
Range("AH1").Select
ActiveSheet.Paste
I have two problems
code get stuck with runtime error 438 at line of in excel 2016
ActiveWorkbook.Worksheets("SHEET").AutoFilter.Sort.SortFields.Add2 Key:=Range _
("C1:C50000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
If I have more than 10000 rows its runs very slowly.
can any one guide me with this please?
Related
I am trying to do two separate sorting of my data according to the dynamic range it falls under.
It is a product order list.
The list has hundreds of products, but a single order may only have x amount of products.
I need to sort the ordered products to the top of the list, and then sort the ordered products to the specifications I require.
There are 2 separate sort functions;
Select the entire range according to values entered in column E, sort by column E.
Select the entire range according to values entered in column E, sort by column C then B.
Thank you :)
I have tried to record a macro and edit it.
No matter what forum/thread solution I have tried, it doesn't give me the correct results.
This is the code of the recorded macro:
Sub SortOrder()
'
' SortOrder Macro
'
Range("A4").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
ActiveWorkbook.Worksheets("Customer Order").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Customer Order").Sort.SortFields.Add2 Key:=Range( _
"E5:E240"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("Customer Order").Sort
.SetRange Range("A4:E240")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Range("E4").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToLeft)).Select
ActiveWorkbook.Worksheets("Customer Order").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Customer Order").Sort.SortFields.Add2 Key:=Range( _
"C5:C95"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
ActiveWorkbook.Worksheets("Customer Order").Sort.SortFields.Add2 Key:=Range( _
"B5:B95"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("Customer Order").Sort
.SetRange Range("A4:E95")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Range("A1").Select
End Sub
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
Here's the code I've written to sort a table based on two columns then deselect the table once that's done. Is there a cleaner way to code this?
Sub SortTable()
' Sorts table
Worksheets("Data").ListObjects("Table").Sort.SortFields. _
Clear
Worksheets("Data").ListObjects("Table").Sort.SortFields. _
Add Key:=Range("Table[Date]"), SortOn:=xlSortOnValues, Order:= _
xlAscending, DataOption:=xlSortNormal
Worksheets("Data").ListObjects("Table").Sort.SortFields. _
Add Key:=Range("Table[Info]"), SortOn:=xlSortOnValues, Order:= _
xlAscending, DataOption:=xlSortNormal
With Worksheets("Data").ListObjects("Table").Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
' Clears table selection
With Worksheets("Data")
.Activate
.Range("A1").Select
End With
End Sub
If you know the position of the columns to sort, then this method seems to clean things up.
With Sheets("data").ListObjects("Table")
.Range.Sort Key1:=.ListColumns(2), Order1:=xlAscending, _
Key2:=.ListColumns(4), Order2:=xlAscending, _
Orientation:=xlTopToBottom, Header:=xlYes
.Parent.Range("A1").Select
End With
For the above, the Date list column is 2 and Info is 4. If you don't know the column ordinals then you need to reference the columns from the parent worksheet.
With Sheets("data").ListObjects("Table")
.Range.Sort Key1:=.Parent.Range("Table[Date]"), Order1:=xlAscending, _
Key2:=.Parent.Range("Table[Info]"), Order2:=xlAscending, _
Orientation:=xlTopToBottom, Header:=xlYes
.Parent.Range("A1").Select
End With
You can do it tidier by moving your With
Sub SortTable()
' Sorts table
With Worksheets("Data").ListObjects("Table").Sort
With .SortFields
.Clear
.Add Key:=Range("Table[Date]"), SortOn:=xlSortOnValues, Order:= _
xlAscending, DataOption:=xlSortNormal
.Add Key:=Range("Table[Info]"), SortOn:=xlSortOnValues, Order:= _
xlAscending, DataOption:=xlSortNormal
End With
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
' Clears table selection
With Worksheets("Data")
.Activate
.Range("A1").Select
End With
End Sub
I have several columns, each has first cell with an Excel date serial number (ex. 42767 which is equal to 2017-02-01). I have to reorder all columns ascending by those dates.
NVM, posting an answer to my own question:
Range("B1").Select
Range(Selection, Selection.End(xlToRight)).Select
ActiveWorkbook.Worksheets("Arkusz1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Arkusz1").Sort.SortFields.Add Key:=Range("B1:C1") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Arkusz1").Sort
.SetRange Range("B1:C8")
.Header = xlGuess
.MatchCase = False
.Orientation = xlLeftToRight
.SortMethod = xlPinYin
.Apply
End With
hello i am having this vb code for sorting macro for my table. I want to make this code continuously updated if the value in column A change (Date). Can anyone help me out?
Sub Macro1()
'
' Macro1 Macro
Range("A6:K6").Select
Range(Selection, Selection.End(xlDown)).Select
ActiveWorkbook.Worksheets("Cash Flow").ListObjects("Table1").Sort.SortFields. _
Clear
ActiveWorkbook.Worksheets("Cash Flow").ListObjects("Table1").Sort.SortFields. _
Add Key:=Range("A6"), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Cash Flow").ListObjects("Table1").Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub