VBA for sorting all data in active sheet(varies in column and row length)

cdalgorta

Board Regular
Joined
Jun 5, 2022
Messages
87
Office Version
  1. 365
Platform
  1. Windows
Hi,

Like the title says, I'm trying to make a VBA to sort "all" my data based on D column(descend). I need a VBA that works on the active sheet regardless of name(always different). Also, the number of columns and rows changes every time I run the report, so I cannot have a specific range.
Basically the same thing that would happen if I clicked on any cell on the new Data, clicked filter, clicked the arrow on D column and clicked largest to smallest. "All" data changes based on D column.

Also, I'd appreciate it a lot if you could give me the same VBA, but if I wanted to have multiple sort criteria like D column descend and then F column Descend. Just like a "custom sort with 2 levels". I don't need it right now, but I will in a few weeks.
Thank you so much in advance!


1658026892789.png
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Try the following:

VBA Code:
Sub SortColumn()
'
    Dim LastRowInColumn         As Long
    Dim StartRow                As Long
    Dim LastColumnInSheet       As String
'
    StartRow = 2
    LastColumnInSheet = Split(Cells(1, (Cells.Find("*", , xlFormulas, , xlByColumns, _
            xlPrevious).Column)).Address, "$")(1)                                               ' Returns a Column Letter
    LastRowInColumn = Range("D" & Rows.Count).End(xlUp).Row                                     ' Returns a Row Number
'
    Range("A" & StartRow & ":" & LastColumnInSheet & LastRowInColumn).Sort _
            Key1:=Range("D" & StartRow), Order1:=xlDescending, Header:=xlNo                      ' Sort Data by Coulmn D
End Sub
 
Upvote 0
Solution
Try the following:

VBA Code:
Sub SortColumn()
'
    Dim LastRowInColumn         As Long
    Dim StartRow                As Long
    Dim LastColumnInSheet       As String
'
    StartRow = 2
    LastColumnInSheet = Split(Cells(1, (Cells.Find("*", , xlFormulas, , xlByColumns, _
            xlPrevious).Column)).Address, "$")(1)                                               ' Returns a Column Letter
    LastRowInColumn = Range("D" & Rows.Count).End(xlUp).Row                                     ' Returns a Row Number
'
    Range("A" & StartRow & ":" & LastColumnInSheet & LastRowInColumn).Sort _
            Key1:=Range("D" & StartRow), Order1:=xlDescending, Header:=xlNo                      ' Sort Data by Coulmn D
End Sub
Thank you so much! It worked perfectly🙏. May I know what would I need to do if I wanted a 2 level sorting for the F column as the 2nd level?
 
Upvote 0
Thank you so much! It worked perfectly🙏. May I know what would I need to do if I wanted a 2 level sorting for the F column as the 2nd level?

VBA Code:
Sub SortTwoColumns()
'
    Dim LastRowInColumn         As Long
    Dim StartRow                As Long
    Dim LastColumnInSheet       As String
'
    StartRow = 2
    LastColumnInSheet = Split(Cells(1, (Cells.Find("*", , xlFormulas, , xlByColumns, _
            xlPrevious).Column)).Address, "$")(1)                                               ' Returns a Column Letter
    LastRowInColumn = Range("D" & Rows.Count).End(xlUp).Row                                     ' Returns a Row Number
'
    Range("A" & StartRow & ":" & LastColumnInSheet & LastRowInColumn).Sort _
            Key1:=Range("D" & StartRow), Order1:=xlDescending, _
            Key2:=Range("F" & StartRow), Order1:=xlDescending, Header:=xlNo                     ' Sort Data by Coulmn D & F
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,117
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top