Sorting a range in Excel with VBA - excel

I have several similar examples that all purport to work, but mine does not. Excel 2013, Office 365.
' Sort the "URLs" worksheet after update
Worksheets("URLs").Activate
lngLastRow = Cells(65536, Range.Column).End(xlUp).Row
Set Range = Worksheets("URLs").Range("A3:E" & lngLastRow)
Worksheets("URLs").Sort.SortFields.Clear
Worksheets("URLs").Sort.SortFields.Add Key:=Range("B4:B" & lngLastRow), _
SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
Worksheets("URLs").Sort.SortFields.Add Key:=Range("A4:A" & lngLastRow), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With Worksheets("URLs").Sort
.SetRange Range("A3:E" & lngLastRow)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Even when I don't use headers (change Range("A3") to "A4") and manually use an end range ("33" instead of lngLastRow), I get the error "Run-time error '5': Invalid procedure call or argument".
I created this macro using the recorder. I don't know why a recorded macro would not work in a macro.
I have never gotten anything with a ":=" to work. I've always had to work around that, but in this case, I can't figure that out either.

Try this:
' Sort the "URLs" worksheet after update
Sub Macro1()
Worksheets("URLs").Activate
Dim Range As Range
Dim lngLastRow As Long
lngLastRow = Worksheets("URLs").Range("A1048576").End(xlUp).Row
Set Range = Worksheets("URLs").Range("A3:E" & lngLastRow)
Worksheets("URLs").Sort.SortFields.Clear
Worksheets("URLs").Sort.SortFields.Add Key:=Worksheets("URLs").Range("B4:B" & lngLastRow), _
SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
Worksheets("URLs").Sort.SortFields.Add Key:=Worksheets("URLs").Range("A4:A" & lngLastRow), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With Worksheets("URLs").Sort
.SetRange Worksheets("URLs").Range("A3:E" & lngLastRow)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub

Related

Getting 1004 ERROR when using double lettered columns (works for single lettered)

I am getting a error when trying to sort these columns. My code for the single lettered columns (ie. "A7") works fine, but when I use "AA7" for instance, there is an error.
Sub SortSectors()
'
' SortSectors Macro
'
'
Dim rng as Range
Set rng = Application.Range("AA7,AA17,AA23,AA33,AA51,AA77,AA101,AA114,AA123,AA131,AA143,AA153,AA178,AA189,AA205,AA212,AA226,AA237,AA241,AA248,AA258,AA263,AA269,AA280,AA292,AA299,AA362,AA376,AA381,AA388,AA404,AA411,AA419,AA427,AA435,AA448,AA467,AA479,AA519,AA545,AA554,AA563,AA573,AA578,AA598")
Dim col As Range
For Each col In rng.Rows
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("AA" & col.Row), _
SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
Range("AA" & col.Row).Activate
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange ActiveCell.CurrentRegion
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Next col
End Sub
There's a 255-character limit to an address in Range... once you used AA as the column instead of A, you surpassed the limit.
But from your current code, it just looks like you need the rows in question; I'd do something like this (currently untested), using an array of just the row numbers:
Dim rowNums as Variant
rowNums = Array(7, 17, 23, 33, 51, 77) ' and so on
Dim i as Long
For i = LBound(rowNums) to Ubound(rowNums)
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SortFields.Clear
.SortFields.Add Key:=Range("AA" & rowNums(i)), _
SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
.SetRange Range("AA" & rowNums(i))
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Next
Note that you really should qualify what workbook/worksheet each Range is in/on.

Error 438 object doesn't support this property

I have this macro that works on my office computer but when I run it at a client's computer It doesn't work and shows the
Error 438 object doesn't support this property
marking this line:
ActiveWorkbook.Worksheets("Hoja1").Sort.SortFields.Add2 Key:=Range("E2:E" & nFilas)
(My excel version is 365 and the client's is 2013)
I've tried changing Worksheets by Sheets, ActiveWorkbook by Sheet but nothing worked.
Columns("E:G").Select
ActiveSheet.Range("$E$1:$G$" & nFilas).RemoveDuplicates Columns:=Array(1, 2, 3), _
Header:=xlYes
Cells.Select
ActiveWorkbook.Worksheets("Hoja1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Hoja1").Sort.SortFields.Add2 Key:=Range("E2:E" & nFilas) _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Hoja1").Sort
.SetRange Range("A1:X" & nFilas)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Change .Add2 into .Add I think .Add2 was introduced in a later Excel version.
I recommend the following improvement:
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Hoja1")
ws.Range("$E$1:$G$" & nFilas).RemoveDuplicates Columns:=Array(1, 2, 3), Header:=xlYes
With ws.Sort
.SortFields.Clear
.SortFields.Add Key:=ws.Range("E2:E" & nFilas), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SetRange ws.Range("A1:X" & nFilas)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

Sort a column in descending order with unknown number of cells

I am trying to sort column A in descending order with unknown number of cells.
Getting run-time error - Method 'Sort' of object'_Worksheet' failed.
This code was working until recently when I got a new laptop with a different version of Excel.
lastrow = Cells(Rows.Count, 1).End(xlUp).Row
Range("A1").Select
ActiveWorkbook.Worksheets("New Working").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("New Working").Sort.SortFields.Add Key:=Range("A1") _
, SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("New Working").Sort
.SetRange Range("A2:AT" & lastrow)
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
The issue might come from the fact that you aren't fully qualifying your Worksheet when getting the value for the Last Row, so Excel would be looking at the ActiveSheet rather than the one you are wanting, I would suggest using something like the code below, where it is fully qualified:
Sub foo()
Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("New Working")
lastrow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
ws.Sort.SortFields.Clear
ws.Sort.SortFields.Add Key:=ws.Range("A1"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
With ws.Sort
.SetRange ws.Range("A1:AT" & lastrow)
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub

Excel Macro to perform multi column sort

I'm trying to create a macro in Excel that will sort spreadsheets by columns A, C and F. I found information online to get me started. Below is the code. The challenge I'm having is, the spreadsheet will contain a different number of rows each day. (The columns will always be the same but the row count will change). The below script will sort my data as long as the spreadsheet does not contain more than 9999 rows (that includes column headings). If I have 10,000 or more rows, the macro fails.
How do I update the below code to allow it to run regardless of the number of rows? Any help you can provide would be appreciated. Thank you
Sub Multi_Sort()
'
' Multi_Sort Macro
'
Dim lngLast As Long
lngLast = Range("A" & Rows.Count).End(xlUp).Row
With Worksheets("Sheet1").Sort
.SortFields.Clear
.SortFields.Add Key:=Range("A1:A1" & lngLast), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SortFields.Add Key:=Range("C1:C1" & lngLast), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SortFields.Add Key:=Range("F1:F1" & lngLast), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SetRange Range("A1:F" & lngLast)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
I can't replicate your error in Excel 2013, but I'm guessing the extra "1" in your range assignments is the problem (i.e., change =Range("A1:A1" & lngLast) to =Range("A1:A" & lngLast), etc.).
Also, a good practice would be to be explicit in your range calls (include the worksheet).
Sub Multi_Sort()
Dim lngLast As Long
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet1")
lngLast = ws.Range("A" & .Rows.Count).End(xlUp).Row
With .Sort
.SortFields.Clear
.SortFields.Add Key:=ws.Range("A1:A" & lngLast), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SortFields.Add Key:=ws.Range("C1:C" & lngLast), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SortFields.Add Key:=ws.Range("F1:F" & lngLast), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SetRange ws.Range("A1:F" & lngLast)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub

Sort Entire Worksheet down to the last row

i have a macro that sorts all my data based on a custom sort but i want to use it on different worksheets that has different last row "number" if so to speak and i have this code here but i keep getting an error:
and just so that u know i am sorting column O
Sub SortDays()
' SortDays Macro
lRow = Worksheets("Banner Summary").Cells(Rows.Count, "B").End(xlUp).Row
Range("B1").Select
Range("A1:A" & lRow).Select
Range("O2").Activate
ActiveWorkbook.Worksheets("Banner Summary").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Banner Summary").Sort.SortFields.Add Key:=Range( _
"O2:O" & lRow), SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:= _
"M,T,W,R,F", DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Banner Summary").Sort
.SetRange Range("A1:A" & lRow)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
The error is: "The sort reference is not valid. Make sure that...."
it is quite long so any help would be really appreciated, and thnx in advance ^_^
Pass in the worksheet you want to sort:
Sub SortDays(byRef ws)
' SortDays Macro
lRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row
'Range("B1").Select
'Range("A1:A" & lRow).Select
'Range("O2").Activate
ws.Sort.SortFields.Clear
ws.Sort.SortFields.Add Key:=Range( _
"O2:O" & lRow), SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:= _
"M,T,W,R,F", DataOption:=xlSortNormal
With ws.Sort
.SetRange Range("A1:O" & lRow)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
Then this will run on any worksheet you pass it (assuming you have defined ws as whichever worksheet you want to use anyway:
Dim ws As Worksheet : Set ws = Workbooks("excelfilename").Worksheets("WhateverSheet")
prior to calling the Sub with SortDays ws

Resources