Sorting Excel table by date and then sending all applicable rows to the bottom

leosandler

New Member
Joined
Oct 5, 2021
Messages
1
Platform
  1. MacOS
The VBA code I have below works for dynamically sorting the items in my Excel Table by date. I want to send all rows with the status marked as "DONE" to the bottom of my table automatically, while ensuring that the rows before are kept in order. The code I have below can dynamically sort dates, but only will send rows with the "DONE" status to the bottom of multiple rows containing the same date (which isn't the desired behaviour, since I want these items at the bottom of the table, not just the bottom of a specific date). Otherwise, the row order stays the same. Any suggestions?

Thanks

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim tbl As ListObject, wRow As Long
  
  Dim Deadlines As ListObject
  Dim SortCol As Range
  Set tbl = ActiveSheet.ListObjects("AllDeadlines")
  Set SortCol = Range("AllDeadlines[Date]")
  
  If Not Intersect(Target, SortCol) Is Nothing Then
    With tbl.Sort
        .SortFields.Clear
        .SortFields.Add Key:=SortCol, Order:=xlAscending
        .Header = xlYes
        .Apply
    End With
End If
  
  If Target.CountLarge > 1 Then Exit Sub
  If Not Intersect(Target, tbl.ListColumns("Status").Range) Is Nothing Then
    If UCase(Target.Value) = "DONE" Then
      Application.ScreenUpdating = False
      wRow = Target.Row - tbl.HeaderRowRange.Row
      tbl.ListRows.Add AlwaysInsert:=True
      tbl.DataBodyRange.Rows(wRow).Copy tbl.DataBodyRange.Rows(tbl.ListRows.Count)
      tbl.ListRows(wRow).Delete
    End If
  End If
End Sub
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.

Forum statistics

Threads
1,215,432
Messages
6,124,860
Members
449,194
Latest member
HellScout

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