Basic sorting: my sub works for sorts on columns with text but for a column with formulas results are the bottom of the range being sorted.

OaklandJim

Well-known Member
Joined
Nov 29, 2018
Messages
906
Office Version
  1. 365
Platform
  1. Windows
Mr. Excelers

I am doing three very basic sorts on a tabular range of data ("database"). Two work as expected and results end up at the top of the data range. BUT I have a third sort for which the results are left at the bottom of the data range. I suspect that the third sort -- involving a column with formulas, not straight text -- needs something added to the sub that does the actual sorting.

There must be a way to sort on a column with formulas so that empty cells are placed below the cells with content.

Here is the sorting sub that I am using to do the actual sort. The sub that produces the bad result when it calls the sorting sub is below that.

VBA Code:
Private Sub DoSort( _
    prSortRangeWithHeaders As Range, _
    piKey1Col As Long, _
    Optional piKey2Col As Long = 1, _
    Optional pbAscending As Boolean = True)
    
'   ----------------------
'       Error Handling
'   ----------------------
    Dim sSubName As String
    Dim sStepID As String
    sStepID = "Initializations"
    sSubName = "DoSort"
    On Error GoTo ErrHandler
'   ----------------------
    
    Dim vEnableStatus As Variant
    
    With Application
        vEnableStatus = .EnableEvents
        .ScreenUpdating = False
        .EnableEvents = False
    End With
    
    Dim vAscending As Variant
    
    If pbAscending _
     Then
        vAscending = xlAscending
    Else
        vAscending = xlDescending
    End If

    sStepID = "Sorting"
        
    prSortRangeWithHeaders.Sort _
        Key1:=prSortRangeWithHeaders.Cells(2, piKey1Col), _
        Key2:=prSortRangeWithHeaders.Cells(2, piKey2Col), _
        Order1:=vAscending, _
        Header:=xlYes

    Application.EnableEvents = vEnableStatus

Exit Sub

ErrHandler:

Application.EnableEvents = True

Call ErrorMessage(Err.Number, Err.Description, sSubName, sStepID)

End Sub

VBA Code:
'Sort on plan types then providers
Sub SortMedPlans_PlanType()

    Dim rSortRange As Range

    Set rSortRange = [MedicalPlans].Range("MedicalPlansData")
    
    Call DoSort(rSortRange, 5, 1)

End Sub
 

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