Sort Entire Worksheet down to the last row - excel

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

Related

How to sort date data type?

I've been trying to sort dates on A1 but it sorts based on text not the value
Private Sub sortData()
Worksheets(Me.Combobox1.Value).Range("A1:F1", Range("A1:F1").End(xlDown)).Sort
Key1:=Range("A1"), Order1:=xlAscending, Header:=xlYes
End Sub
As #Marcelo Scofano Diniz started to say, when you run into a problem forget about the shortcxuts and go by the book. Here is the book.
Private Sub SortByDate()
Dim Ws As Worksheet
Dim SortRange As Range
Set Ws = Worksheets(Me.ComboBox1.Value)
With Ws
Set SortRange = .Range(.Cells(1, 1), .Cells(.Rows.Count, "A").End(xlUp)) _
.Resize(, 6)
With .Sort.SortFields
.Clear
.Add2 Key:=SortRange.Cells(1), _
SortOn:=xlSortOnValues, _
Order:=xlAscending, _
DataOption:=xlSortNormal
End With
With .Sort
.SetRange SortRange
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End With
End Sub
The above code will carry out the sort you say your code won't do. If the dates are true dates they will be sorted by date. If they are fake dates they will be sorted as text. And if you have doubt about any part of the code you can see what is being done and step through it line by line.
Change The Range to Table of Name Tbl
then Create :
Sub Table_Sort(byval FieldNo as Integer)
Tbl.Sort.SortFields.Clear
FieldRange(FieldNo).Select
Tbl.Sort.SortFields.Add _
Key:=FieldRange(FieldNo), SortOn:=xlSortOnValues, Order:= _
xlAscending, DataOption:=xlSortNormal
With Tbl.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub

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

How to sort cells in a column from A to Z?

The code is from slightly edited macro. I tried to remove the 'messy code', however it is not working. The aim of it is to sort data in column BF from A to Z.
Dim InSheet As Worksheet
Set InSheet = ThisWorkbook.Worksheets("A to Z")
Dim LastRow as Integer
LastRow = InSheet.Cells(Rows.Count, 58).End(xlUp).Row
InSheet.Select
Columns("BF:BF").Select
InSheet.Sort.SortFields.Clear
InSheet.Sort.SortFields.Add Key:=Range( _
"BF1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With InSheet.Sort
.SetRange Range("A1:BF" & LastRow)
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
SO I tried this and it is not working:
Dim InSheet As Worksheet
Set InSheet = ThisWorkbook.Worksheets("A to Z")
Dim LastRow as Integer
LastRow = InSheet.Cells(Rows.Count, 58).End(xlUp).Row
InSheet.Columns("BF:BF")
InSheet.Sort.SortFields.Clear
InSheet.Sort.SortFields.Add Key:=Range( _
"BF1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With InSheet.Sort
.SetRange Range("A1:BF" & LastRow)
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Any ideas? I would like to really avoid .select an other stuff as it is hugely decreasing performance.
You can just directly sort the Range with a Key with out the rest of the code you have.
Range("A1:BF" & LastRow).SORT Key1:=Range("BF1"), Order1:=xlAscending_
Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:= _
xlTopToBottom, DataOption1:=xlSortNormal
I used advice provided by both Steven Martin's answer and Dmitry Pavliv's comment, and this is working pretty well:
Dim InSheet As Worksheet
Set InSheet = ThisWorkbook.Worksheets("A to Z")
Dim LastRow As Integer
LastRow = InSheet.Cells(Rows.Count, 58).End(xlUp).Row
With InSheet.Sort ' sort data from A to Z
.SetRange InSheet.Range("A1:BF" & LastRow)
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

Sorting a range in Excel with VBA

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

How can I only sort thru the rows with data?

I have a macro that sorts the rows by a specific column, the problem is I'm manually setting the range for the number of rows to sort (i.e. A2:A174) every time I add a new row. How can I change my code so that it sorts all the rows with data only so I don't have to go into the code and change the range every time I add a new row.
Sub SortByName()
SortByName Macro
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("A2:A174") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("A1:H174")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Range("A1").Select
End Sub
Sub SortByDate()
Thanks so much in advance for your wisdom!
something like this which looks for the last cell in A to mark the range.
Sub SortByName()
Dim ws As Worksheet
Dim rng1 As Range
Set ws = Sheets("Sheet1")
Set rng1 = ws.Range(ws.[a1], Cells(Rows.Count, "A").End(xlUp))
ws.Sort.SortFields.Clear
ws.Sort.SortFields.Add Key:=rng1 _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange rng1.Resize(rng1.Rows.Count, 8)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Application.Goto ws.[a1]
End Sub

Resources