Auto Sort with Blank Formulas

OX_2005

New Member
Joined
Feb 29, 2024
Messages
43
Office Version
  1. 365
Platform
  1. Windows
I am trying to auto sort by Ascending order in the name column of this table. I currently have a formula set up in that column. The code I have written is working but it is putting the Blanks at the top (I am assuming its because they have formulas in them). Is there a way I can have it sort in Ascending order and leave the blanks at the bottom? (Current code below)

1713453174570.png



VBA Code:
Sub SortRosterA()

Dim RA As Worksheet
Set RA = ThisWorkbook.Sheets("Shift Roster T-A 1 3")

'   Unprotect sheet

    RA.Unprotect
   
'   Not Allow screen to update

 Application.ScreenUpdating = False
 
'   Auto Sort

    With RA.ListObjects("DirectA").Sort
        .SortFields.Clear
        .SortFields.Add Key:=Range("DirectA[Name]"), Order:=xlAscending
        .Apply
    End With



End Sub
 

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)
The following macro will add a temporary column, then it will sort based on the temporary column, and then it will delete the temporary column...

VBA Code:
Sub SortRosterA()

'   Not Allow screen to update
    Application.ScreenUpdating = False
 
    Dim RA As Worksheet
    Set RA = ThisWorkbook.Sheets("Shift Roster T-A 1 3")

'   Unprotect sheet
    RA.Unprotect
 
    Dim tempListColumn As ListColumn
    Set tempListColumn = RA.ListObjects("DirectA").ListColumns.Add
   
    Dim sortListColumn As ListColumn
    Set sortListColumn = RA.ListObjects("DirectA").ListColumns("Name")
 
    Dim rowIndex As Long
    With sortListColumn.DataBodyRange
        For rowIndex = 1 To .Rows.Count
            If Len(.Cells(rowIndex)) = 0 Then
               tempListColumn.DataBodyRange.Cells(rowIndex).Value = "zzzzz"
            Else
                tempListColumn.DataBodyRange.Cells(rowIndex).Value = .Cells(rowIndex).Value
            End If
        Next rowIndex
    End With

'   Auto Sort
    With RA.ListObjects("DirectA").Sort
        .SortFields.Clear
        .SortFields.Add Key:=tempListColumn.Range, Order:=xlAscending
        .Apply
    End With
   
    tempListColumn.Delete
   
    Application.ScreenUpdating = True

End Sub

Hope this helps!
 
Upvote 0
The following macro will add a temporary column, then it will sort based on the temporary column, and then it will delete the temporary column...

VBA Code:
Sub SortRosterA()

'   Not Allow screen to update
    Application.ScreenUpdating = False
 
    Dim RA As Worksheet
    Set RA = ThisWorkbook.Sheets("Shift Roster T-A 1 3")

'   Unprotect sheet
    RA.Unprotect
 
    Dim tempListColumn As ListColumn
    Set tempListColumn = RA.ListObjects("DirectA").ListColumns.Add
  
    Dim sortListColumn As ListColumn
    Set sortListColumn = RA.ListObjects("DirectA").ListColumns("Name")
 
    Dim rowIndex As Long
    With sortListColumn.DataBodyRange
        For rowIndex = 1 To .Rows.Count
            If Len(.Cells(rowIndex)) = 0 Then
               tempListColumn.DataBodyRange.Cells(rowIndex).Value = "zzzzz"
            Else
                tempListColumn.DataBodyRange.Cells(rowIndex).Value = .Cells(rowIndex).Value
            End If
        Next rowIndex
    End With

'   Auto Sort
    With RA.ListObjects("DirectA").Sort
        .SortFields.Clear
        .SortFields.Add Key:=tempListColumn.Range, Order:=xlAscending
        .Apply
    End With
  
    tempListColumn.Delete
  
    Application.ScreenUpdating = True

End Sub

Hope this helps!
Will this remove any Formulas I have in place?
 
Upvote 0
When a new column is added to the table, it tries to insert cells to the right of it. However, in your case, it won't be able to do so, since it would mean that only part of Team 3 table would be moved, and you can't move part of a table.

One workaround would be to move your table (Team A Direct) to the right of your other tables. Woud that be acceptable to you?
 
Upvote 0
When a new column is added to the table, it tries to insert cells to the right of it. However, in your case, it won't be able to do so, since it would mean that only part of Team 3 table would be moved, and you can't move part of a table.

One workaround would be to move your table (Team A Direct) to the right of your other tables. Woud that be acceptable to you?

That puts a damper in things. So the end game of this Macro is that when the button is pressed it will auto sort all tables on the page.
 
Upvote 0
Sure, it's understandable that you'd like to keep the layout unchanged.

Do these blank cells always occur at the end of the table before sorting? Or can they occur anywhere within the data? If the former, we can exclude those rows from the sort.
 
Upvote 0
Sure, it's understandable that you'd like to keep the layout unchanged.

Do these blank cells always occur at the end of the table before sorting? Or can they occur anywhere within the data? If the former, we can exclude those rows from the sort.
The blank cells can sometimes be in the middle or at the end it depends on how many people we move around on shifts and then these get printed and thats how they take attendance
 
Upvote 0

Forum statistics

Threads
1,223,884
Messages
6,175,175
Members
452,615
Latest member
bogeys2birdies

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