Sort leaves empty row before last entry in sort range

OaklandJim

Well-known Member
Joined
Nov 29, 2018
Messages
833
Office Version
  1. 365
Platform
  1. Windows
Team Mr. Excel(lent).

I have a five column range that I sort. My sort routine sizes the sort range's rows based on the count of entries. Sorting works fine "normally." BUT, after deleting (blanking out using .ClearContents) one of the entries I call my sort Sub. (Entries count is now one less after deletion.) But, after doing the deletion of data in one row of the sort range the sort leaves an empty row before the last entry. I tried adding an extra row or two to the row count for the sort range but I still get an empty row before the last entry in the data range. What Am I missing? What should I try? Here is my sort sub:

VBA Code:
Sub SortStaff_Position_LName_FName( _
    Optional pbNamesOnly As Boolean = False, _
    Optional pbAddExtraRow As Boolean = False)
'
    Dim rSortRange As Range
    Dim iColumnsToSort_Staff As Integer
    Dim iRowsToSort As Integer
    
    iColumnsToSort_Staff = 5 '1. LName, 2. FName, 3, hourly rate, 4. position, 5. tip share weight
    
    With [Staff]
        iRowsToSort = .Range("Staff_Entries_Count").Value + 1 '+1 for header row

        If pbAddExtraRow Then iRowsToSort = iRowsToSort + 1 'after deleting an entry.

'       Range of cells to be sorted.
        Set rSortRange = .Range("Header_Last_Name").Resize(iRowsToSort, iColumnsToSort_Staff)

        With .Sort
        
            With .SortFields
                .Clear
                
                If Not pbNamesOnly _
                 Then
                    .Add2 Key:=Range("dPositions_Staff") _
                    , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
                End If
                
                .Add2 Key:=Range("dLastNames_Staff") _
                , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
                
                .Add2 Key:=Range("dFirstNames_Staff") _
                , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
                
            End With '.SortFields

            .SetRange rSortRange
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
           
        End With '.Sort

    End With '[Staff]
    
End Sub
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
OK I already figured out my issue. I use dynamically sized ranges (e.g., dFirstNames) which are sized -- based on the number of entries -- so they are one row shy of the entire range that is to be sorted after my deletion.
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,868
Members
449,053
Latest member
Mesh

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