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
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 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?
Can the cells be sorted on the basis of the colors allotted to a column instead of A-Z or Z-A?
Not sure exactly what you are looking for but something like this will soft by color. You are looking for SortOn:=xlSortOnCellColor
Columns("C:C").Select
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("B:B"),SortOn:=xlSortOnCellColor, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("C2:C6")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
EDIT PER OPs COMMENT: If you just want a sort.
Pick the custom sort option and select sort on Cell color.
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
I am trying to sort multiple columns of a certain range. My sheet has 4 columns (A,B,C,D) and 80 rows... however, I want to sort by column D. However, I only want to sort in descending order between rows 2-20. Can someone please help me with this code?
Here is the code I have:
Sub Macro1()
Range("A2:D20").Select
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("D2:D20") _
, SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("A1:D20")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
Thank you in advance.
How about just:
Range("A2:D20").Sort key1:=Cells(20, 4), order1:=xlDescending, Header:=xlNo