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
Related
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
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
hello i am having this vb code for sorting macro for my table. I want to make this code continuously updated if the value in column A change (Date). Can anyone help me out?
Sub Macro1()
'
' Macro1 Macro
Range("A6:K6").Select
Range(Selection, Selection.End(xlDown)).Select
ActiveWorkbook.Worksheets("Cash Flow").ListObjects("Table1").Sort.SortFields. _
Clear
ActiveWorkbook.Worksheets("Cash Flow").ListObjects("Table1").Sort.SortFields. _
Add Key:=Range("A6"), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Cash Flow").ListObjects("Table1").Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
Have put together a macro to sort a column from ascending and descending order (posted below), But I am interested in a macro that can sort ascending to descending numerical order but keep the other rows of data corresponding with the ascending and descending data that has moved on my excel sheet. Anyone know the macro for this scenario?
Private Sub CommandButton1_Click()
If CommandButton1.Caption = "Click to Sort Ascending" Then
'Sort ascending...
ActiveWorkbook.Worksheets("daily data drop").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("daily data drop").Sort.SortFields.Add Key:=Range("A2:A36"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("daily data drop").Sort
.SetRange Range("A2:A36")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
CommandButton1.Caption = "Click to Sort Decending"
Else
'sort decending
ActiveWorkbook.Worksheets("daily data drop").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("daily data drop").Sort.SortFields.Add Key:=Range("A2:A36") _
, SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("daily data drop").Sort
.SetRange Range("A1:A36")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
CommandButton1.Caption = "Click to Sort Ascending"
End If
End Sub
Try to change this line
.SetRange Range("A1:A36")
to this
.SetRange Range("A1:B36")
or change "B" to what ever column letter you go up to
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