VBA & Macro Problems

Sort Blank Cells to the Bottom in Excel VBA

Natalie TaylorNatalie Taylor Nov 18, 2026 868 views

Question details

Sort rows in Excel with populated column C values first and blank column C cells last.

Sort Blank Cells to the Bottom in Excel VBA
Product
Microsoft Excel VBA
Device & OS
not provided
Scenario
A macro sorts a table in columns A:F using values in column C.
Observed behavior
Blank column C cells appear at the top instead of below populated rows.
Before you start

Identify the header and last data row used by your existing macro, and check that column G is unused within those rows. The example assumes headers are in row 1 and data starts in row 2.

Solution 1Recommended

Add a helper column to the Excel VBA sort

Sort a temporary marker first, then sort column C, so rows with blank column C cells move to the bottom.

The helper column must be included in the sort range so its markers move with the rows. The example temporarily sorts A:G, then clears the markers; the original data remains in A:F.

Replace the example lastRow value with the last row already used by your macro. If column G contains data, choose another unused helper column and include all columns through that helper in the sort range; those included columns will also be reordered.

1
Open the VBA editor

In Excel, press Alt+F11 and open the module containing your sort macro.

2
Add the helper formula and sort keys

Replace the relevant sort section with the following example, adjusting the worksheet and lastRow to match your existing macro: Dim ws As Worksheet Dim lastRow As Long Set ws = ActiveSheet lastRow = 100 ' Replace with your existing last data row ws.Range("G2:G" & lastRow).Formula = "=IF(C2="""","""",""Data"")" With ws.Sort .SortFields.Clear .SortFields.Add Key:=ws.Range("G2:G" & lastRow), _ SortOn:=xlSortOnValues, Order:=xlDescending .SortFields.Add Key:=ws.Range("C2:C" & lastRow), _ SortOn:=xlSortOnValues, Order:=xlAscending .SetRange ws.Range("A1:G" & lastRow) .Header = xlYes .Apply End With ws.Range("G2:G" & lastRow).ClearContents

3
Run and check the sort

Run the macro from the VBA editor or Excel's Macros dialog. Check that rows with values in column C appear first in ascending order and that the other cells in each row stayed together.

Add a helper column to the Excel VBA sort
Keep the sort range consistent: Use the same first and last data rows as your original A:F sort. The helper range and column C key must cover those same data rows.
Free Microsoft Office alternative

Work with sorted spreadsheets in WPS Office

WPS Office is a free, lightweight Office alternative with a familiar interface for opening and sorting spreadsheets. Its Excel format compatibility can make moving existing files straightforward, although Excel VBA macros may not run the same way in WPS.

Sort spreadsheet data using a familiar interfaceOpen and edit Microsoft Excel XLSX filesUse a free, lightweight office suiteMove existing spreadsheet workflows to a familiar layout
microsoft office alternative - wps office

Frequently Asked Questions

Why does the helper column need to be in the sort range?

The marker must move with its row when Excel sorts. Include the helper column in the VBA .SetRange, such as A1:G100 when G is the helper.

Can I use column Z instead of column G?

Yes. Put the marker formula in column Z, use Z as the first sort key, and extend .SetRange through column Z. Be aware that columns G:Y within that range will also be reordered.

What happens if a cell in column C contains a formula returning an empty string?

The helper formula =IF(C2="","","Data") treats a formula result of an empty string as blank, so that row is grouped with other blank column C rows.

How do I keep column headers out of the sort?

Include the header row in .SetRange and set .Header = xlYes. The example uses row 1 for headers and starts both sort keys in row 2.