OaklandJim
Well-known Member
- Joined
- Nov 29, 2018
- Messages
- 833
- Office Version
- 365
- Platform
- 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:
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