In the VBA code below, the active sheet rows are sorted by column I. After column I has…
- User
In the VBA code below, the active sheet rows are sorted by column I. After column I has been sorted, is it possible to also the rows by column H in a decending order and to make sure that any blank cells in column H so that the blank cells are always at the top
Public Sub SortTasksByDate() Application.EnableEvents = False Application.ScreenUpdating = False Dim lastRow As Long lastRow = ActiveSheet.Range("J" & Rows.count).End(xlUp).Row With Range("B5:L" & lastRow) .Sort Key1:=ActiveSheet.Range("I5:I" & lastRow), Order1:=xlAscending, Header:=xlYes End With Application.ScreenUpdating = True Application.EnableEvents = True End Sub
- Assistant
Yes, it is possible to sort the rows by column H in a descending order and move the blank cells to the top after sorting by column I.
To achieve this, you can modify the code as follows:
Public Sub SortTasksByDate() Application.EnableEvents = False Application.ScreenUpdating = False Dim lastRow As Long Dim rng As Range
codelastRow = ActiveSheet.Range("J" & Rows.Count).End(xlUp).Row Set rng = Range("B5:L" & lastRow) ' Sort by column I in ascending order With rng .Sort Key1:=ActiveSheet.Range("I5:I" & lastRow), Order1:=xlAscending, Header:=xlYes End With ' Sort again by column H in descending order, moving blank cells to the top With rng .Sort Key1:=ActiveSheet.Range("H5:H" & lastRow), Order1:=xlDescending, Header:=xlYes, _ OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _ DataOption1:=xlSortNormal End With Application.ScreenUpdating = True Application.EnableEvents = True