If this is my only sort statement in my sub then can I just delete SortFields.Clear?
Any useful applications of using SortFields.Clear?
I don't think I have any existing sort fields at the start of my code. Or in the actual worksheet.
Code in question
With ws_raw.Sort
.SortFields.Clear
.SortFields.Add2 Key:=Range("A1"), Order:=xlAscending
.SortFields.Add2 Key:=Range("L2"), Order:=xlDescending
.SetRange rng_raw
.Header = xlYes
.Apply
End With
What I am trying to accomplish, looking for a similar code:
With ws_raw.Sort
.SortFields.Add2 Key:=Range("A1"), Order:=xlAscending
.SortFields.Add2 Key:=Range("L2"), Order:=xlDescending
.SetRange rng_raw
.Header = xlYes
.Apply
End With
' main code
With ws_raw.Sort
.SortFields.Clear
.Header = xlYes
.Apply
End With
Related
My worksheet has a large table with five columns. Row 1 has headers. I would like to sort the entire table using four column headers as sort fields.
VBA only allows three, so I tried the following
Sub CreateTimeSeries()
With Range("A1", Range("A1").SpecialCells(xlCellTypeLastCell)).Sort
.SortFields.Clear
.SortFields.Add Key:=Range("B1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SortFields.Add Key:=Range("A1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SortFields.Add Key:=Range("C1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SortFields.Add Key:=Range("D1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.Header = xlYes
.Apply
End With
End Sub
I get
Run-time error '1004':
Unable to get the Sort property of the Range class.
It can't be the worksheet. I select the worksheet before running the routine. Trying various solutions on StackOverflow has not fixed the error.
This works for me:
Sub CreateTimeSeries()
Dim tbl As Range
With ActiveSheet
Set tbl = .Range("A1", .Range("A1").SpecialCells(xlCellTypeLastCell))
End With
With tbl.Parent.Sort
.SortFields.Clear
.SortFields.Add Key:=Range("B1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SortFields.Add Key:=Range("A1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SortFields.Add Key:=Range("C1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SortFields.Add Key:=Range("D1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SetRange tbl '<<<<<<<
.Header = xlYes
.Apply
End With
End Sub
I can do some basic sortinng using vba however i cannot get things to work when i want to make it dynamic...
what i have done now is set a hard stop at 10k, however the workbooks i need to sort change rapidly in size.
what would be the best approach and why?
i have tried to set the "Range" to Range("a1:B" & lastrow), respectively for each select however this seems to hang and not execute the macro.
ActiveWorkbook.Worksheets("Rapportage").Sort.SortFields.Add2 Key:=Range( _
"A1:BZ1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("Rapportage").Sort
.SetRange Range("A1:BZ9999")
.Header = xlYes
.MatchCase = False
.Orientation = xlLeftToRight
.SortMethod = xlPinYin
.Apply
End With
Application.CutCopyMode = False
Range("q3", Range("q5 : q9999")).FormulaR1C1 = "=RC[1]+RC[2]"
Rows("5:9999").Select
ActiveWorkbook.Worksheets("Rapportage").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Rapportage").Sort.SortFields.Add2 Key:=Range( _
"B5:B9999"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
ActiveWorkbook.Worksheets("Rapportage").Sort.SortFields.Add2 Key:=Range( _
"C5:C9999"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
ActiveWorkbook.Worksheets("Rapportage").Sort.SortFields.Add2 Key:=Range( _
"F5:F9999"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
ActiveWorkbook.Worksheets("Rapportage").Sort.SortFields.Add2 Key:=Range( _
"E5:E9999"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
ActiveWorkbook.Worksheets("Rapportage").Sort.SortFields.Add2 Key:=Range( _
"I5:I9999"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortTextAsNumbers
With ActiveWorkbook.Worksheets("Rapportage").Sort
.SetRange Range("A5:BZ9999")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With```
I think your code should look somewhat like the procedure below. Note that the Key defines the column to be sorted on. It's specified by a single cell in it, usually in the first row but I chose the fifth because the first row is excluded from your range - not that VBA or Excel would take offence.
Sub TestSort()
' 041
Dim Cell As Range
Dim SortRng As Range
With ActiveWorkbook.Worksheets("Rapportage")
Set Cell = .Cells(5, 2)
With .Sort.SortFields
.Clear
.Add Key:=Cell, SortOn:=xlSortOnValues, _
Order:=xlAscending, DataOption:=xlSortNormal
.Add Key:=Cell.Offset(, 1), SortOn:=xlSortOnValues, _
Order:=xlAscending, DataOption:=xlSortNormal
.Add Key:=Cell.Offset(, 4), SortOn:=xlSortOnValues, _
Order:=xlAscending, DataOption:=xlSortNormal
.Add Key:=Cell.Offset(, 3), SortOn:=xlSortOnValues, _
Order:=xlAscending, DataOption:=xlSortNormal
.Add Key:=Cell.Offset(, 7), SortOn:=xlSortOnValues, _
Order:=xlAscending, DataOption:=xlSortNormal
End With
Set SortRng = .Range(.Cells(5, "A"), _
.Cells(.Rows.Count, "A").End(xlUp).Offset(, 77))
With .Sort
.SetRange SortRng
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End With
End Sub
Specifying the column to sort on is different from specifying the range to be sorted. That enters the code only after the keys have all been set and the actual sorting process is initiated.
I'm trying to write a code that will remove duplicates from my list.
It worked well on my mac, but when I tried to run it on windows I got the runtime error 438:
object doesn't support this property or method.
What went wrong?
Range("E2:E150").Select
ActiveWorkbook.Worksheets("NOS").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("NOS").Sort.SortFields.Add2 Key:=Range("E2"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("NOS").Sort
.SetRange Range("E2:G150")
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.Apply
End With
Range("E1").Select
End Sub
The issue is that you probably use an Excel version released before 2016. The .SortFields.Add2 method was introduced in 2016 if your Excel is older you must use the old .SortFields.Add method (which lacks the ability of using the SubField parameter).
You might benefit from reading How to avoid using Select in Excel VBA, your .Select statements are pretty useless and not needed.
With ActiveWorkbook.Worksheets("NOS").Sort
.SortFields.Clear
.SortFields.Add Key:=Range("E2"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SetRange Range("E2:G150")
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.Apply
End With
When I try to execute my VBScript using this line of code (which is taken from Microsoft's own docs) I get the following error:
Microsoft VBScript compilation error Expected statement
IActiveScriptSite::OnScriptError()
Line: .SortFields.add Key:=Range("E2:E11"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
Here is the code:
With xlWorkSheet.Sort
.SortFields.Clear
.SortFields.Add Key:=Range("E2:E11"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SetRange Range("A2:A11")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Taken from Microsofts docs
After incorporating suggestions from Rory, the following code is now throwing a runtime error "invalid procedure call or argument"
With xlWorkSheet.Sort
.SortFields.Clear
.SortFields.Add xlWorkSheet.Range("E2:E10"), 0, 1, ,0
.SetRange xlWorkSheet.Range("A2:A10")
.Header = 2
.MatchCase = False
.Orientation = 0
.SortMethod = 0
.Apply
End With
VBScript doesn't have named arguments. You have to specify them by position and remove the named parts.
.SortFields.Add xlworksheet.Range("E2:E11"), xlSortOnValues, xlAscending, , xlSortNormal
I'm assuming you have declared the constants somewhere. If not, either do so, or use the literal values:
.SortFields.Add xlworksheet.Range("E2:E11"), 0, 1, , 0
Note that you have to leave the gap for the missing DataOption argument that you aren't providing.
I have wrote the following code to sort data on my workbook
With gwksSheet
With .Sort
.SortFields.Clear
.SortFields.Add Key:=Columns(glAssetTypeCol), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SortFields.Add Key:=Columns(glFundCodeCol), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SortFields.Add Key:=Columns(glOberonCol), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SetRange Range(Cells(1, 1), Cells(glLastRow, glLastCol))
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
In the column "glOberonCol" I have numbers (e.g. 2561236) & Text (e.g. 2561236R). When it sorts it sorts the numbers and then the text. I want to sort it so I get 2561236 & 2561236R beside each other. What do I need to do. Appreciate any help.
Thanks,
Ciaran.
Follow the steps bellow:
Create another column
Add formula "=TEXT(RC[-1],"###")"
Copy this value and override the old values forcing Excel to see numbers as strings.
Now you can use your macro or just use Filter button.