79083440

Date: 2024-10-13 14:54:23
Score: 1
Natty:
Report link

Thank you so much everyone for taking the time to add your comments and answers. They helped point me in the right direction. Indeed @Michal's answer works for a fixed number of columns and many may find it useful. What was needed was specifically a VBA code for a selection of columns, whenever a user arbitrarily selects any range of cells spanning any number of columns and have them sorted correctly.

After some research, this crafted solution works and works for anyone.

Sub SortBlanksOnTop()
    On Error Resume Next
    
    ' Set up your variables and turn off screen updating.
      Dim w As Worksheet
      Dim r As Range ' Working range
      Dim v As Double ' Value for blank cells
      Application.ScreenUpdating = False
    
    ' Set title for the range selection user dialog box.
      t = "Selection Range"
    
    ' Get the sheet.
      Set w = ActiveSheet
    
    ' Request and store range from user.
      Set r = Application.InputBox("Range", t, Application.Selection.Address, Type:=8)
    
    ' Create a new least value by subtracting from the least value in the selection.
    ' Doubles are being used because Ms Excel sorting ranks them before alphabets in ascending order.
      v = Application.WorksheetFunction.Small(r, 1) - 1
    
    'Substitute all blank cells in the selection range with this new value.
      r.SpecialCells(xlCellTypeBlanks) = v
    
    ' Clear any existing sort fields.
      w.Sort.SortFields.Clear

    ' Add sort field for each column in the selection range.
      For Each Column In r.Columns
         w.Sort.SortFields.Add Key:=Column, Order:=xlAscending
      Next Column

    ' Apply the sort
      With w.Sort
        .SetRange r
        .Header = xlNo
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
      End With
            
    ' Revert to blank cells.
      r.Replace What:=v, Replacement:="", LookAt:=xlWhole
      
    ' Turn screen updating back on.
      Application.ScreenUpdating = True
End Sub

Feel free to add conditions in the For Each loop to exclude any kinds of cells, columns or rows.

Hope to save someone's time.

Reasons:
  • Blacklisted phrase (0.5): Thank you
  • Long answer (-1):
  • Has code block (-0.5):
  • User mentioned (1): @Michal's
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: John Miller