Use variable as .setrange property - excel

I am trying to use this code to sort a spreadsheet in excel but I am unsure how to put a predefined range in the .SetRange property. I wish to use the rng1Row variable I set above. Any help would be greatly appreciated.
N = Cells(Rows.Count, "A").End(xlUp).Row
Set rng1 = wb1.Sheets("SourceData").Cells.Range("A2:A" & N)
Set rng1Row = rng1.EntireRow
wb1.Worksheets("SourceData").Sort.SortFields.Clear
wb1.Worksheets("SourceData").Sort.SortFields.Add Key:=wb1.Sheets("SourceData").Cells.Range("A2:A" & N), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With wb1.Worksheets("SourceData").Sort
.SetRange
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

By using .SetRange rng1Row you are only sorting 1 row.
Change to:
.SetRange wb1.Sheets("Source Data").Range("A2").CurrentRegion
Or similar to select the entire table of values.

Related

how can i sort column headers along with values A to Z (Alphabetically) with VBA in Excel

I am trying to run the macro from sheet1 to sort the column by headers A to Z using VBA in sheet2. Anyone can help me to find it out.
Sub Macro1()
Range("C10:K13").Select
ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Add Key:=Range("C10:K10") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet2").Sort
.SetRange Range("C10:K13")
.Header = xlYes
.MatchCase = False
.Orientation = xlLeftToRight
.SortMethod = xlPinYin
.Apply
End With
End Sub
As far as I know you can't sort horizontally so I transformed your range into vertical to sort and return the original value after sorting through the transpose function, this is my approach:
Sub Test()
Dim rng As Range, transposeRng As Range
Set rng = sheets("Sheet2").[C10:K10]: Set transposeRng = sheets("Sheet2").[M6].Resize(rng.Columns.Count, rng.Rows.Count)
transposeRng.Value = Application.Transpose(rng)
ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Add2 Key:=transposeRng.Columns(1) _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet2").Sort
.SetRange transposeRng
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
transposeRng.Clear
rng.Value = Application.Transpose(transposeRng)
End Sub

VBA Excel Column Sorting Run Time Error 1004

I want to sort column A without using select but I keep Getting error massage,
Unable to get the sort property of the range class
'Sort Managers
With QueWS.Columns("A:A")
.Sort.SortFields.Clear
.Sort.SortFields.Add Key:=Range("A2:A" & LastRow), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Que").Sort
.SetRange Range("A1", Cells(LastRow, LastColumn))
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End With
Try this
With ActiveWorkbook.Worksheets("Que")
.Sort.SortFields.Clear
.Sort.SortFields.Add Key:=Range("A1"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Que").Sort
.SetRange Range("A2:A" & LastRow)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End With

sort macro in Excel

I am trying to use this code to sort a spreadsheet in excel but I am unsure how to put a predefined range in the .SetRange property. I wish to use the rng1Row variable I set above. Any help would be greatly appreciated.
N = Cells(Rows.Count, "A").End(xlUp).Row
Set rng1 = wb1.Sheets("SourceData").Cells.Range("A2:A" & N)
Set rng1Row = rng1.EntireRow
wb1.Worksheets("SourceData").Sort.SortFields.Clear
wb1.Worksheets("SourceData").Sort.SortFields.Add Key:=wb1.Sheets("SourceData").Cells.Range("A2:A" & N), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With wb1.Worksheets("SourceData").Sort
.SetRange
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Here is the documentation: expression .SetRange(Rng)
SetRange Range(rng1Row) (assuming rng1Row is a range)
However, let's look at the first part of your code -
N = Cells(Rows.Count, "A").End(xlUp).Row
Set rng1 = wb1.Sheets("SourceData").Cells.Range("A2:A" & N)
Set rng1Row = rng1.EntireRow
You are trying to get the row of a column. rng1Row = (Range("A:A").EntireRow). This is not a valid use of the property.

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

Resources