Excel VBA Sort Range

This guide treats “excel vba sort range” the way busy spreadsheet users actually want it: answer first, then the reasoning. It is written for Excel but calls out every place Google Sheets differs, and the platform toggle at the top switches all shortcuts between Windows and Mac so nothing here assumes the keyboard you are not on.

Exact answer

In Excel: paste Sub SortRangeVBA() into the VBA editor (Alt + F11Insert ▸ Module), press F5 to run, and save as .xlsm to keep the macro.

VBA macro: Sort a Range with VBA

Sub SortRangeVBA()
    Dim ws As Worksheet
    Dim sortRange As Range
    Set ws = ActiveSheet
    Set sortRange = ws.Range("A1:C20")
    sortRange.Sort Key1:=ws.Range("A1"), _
        Order1:=xlAscending, _
        Header:=xlYes, _
        DataOption1:=xlSortNormal
End Sub

Key1 sets the sort column (here column A). Change Order1 to xlDescending for Z→A / largest-to-smallest. Add Key2 and Order2 for a secondary sort key.

How to run this macro

  1. Press Alt + F11 to open the VBA editor.
  2. Insert > Module.
  3. Paste the code above.
  4. Press F5, or close the editor and run it from Developer > Macros.
  5. Save the file as .xlsm so the macro is kept.
Annotated stepsExcel
1

Open Excel and navigate to Developer ▸ Visual Basic (or press Alt + F11).

2

Select Insert ▸ Module from the menu bar inside the VBA editor.

3

Copy and paste Sub SortRangeVBA() into the Module.

4

Click anywhere inside the Sub body and press F5 to run.

5

Close the VBA editor and save the file as .xlsm.

Ctrl+CthenCtrl+Shift+V+Cthen+Ctrl+VPaste values · WindowsMac

What this does

Sub SortRangeVBA() is a ready-to-run VBA macro that handles sort a data range using a VBA macro without manual effort. VBA (Visual Basic for Applications) is Excel's built-in automation language: it can read every cell, loop through thousands of rows, and make decisions in milliseconds. To use it, open the VBA editor with Alt + F11, create a Module, paste the code, and press F5. The Sort a Range with VBA macro will run immediately — no add-ins, no external tools, just Excel doing exactly what the code tells it to do. The same idea underpins a lot of everyday Excel work, so the few minutes spent getting it right here pay back across every sheet you build afterwards. Treat it as a pattern, not a one-off, and it stops being something you look up and starts being something you reach for. Treat “excel vba sort range” as a small repeatable workflow rather than a one-off click you hope to remember next time. Use a small test block before the live file, so any surprise in the affected cells shows up while it is still harmless. When this is a ribbon command, the selection matters more than the button: confirm the range, apply the command, then spot-check the output before saving. That turns a data step that keeps your analysis trustworthy into a method you can reuse, explain, and defend when the workbook leaves your screen.

A worked example

Practical use: you run the same sort a data range using a VBA macro task every month on a new export. Instead of repeating the steps manually, paste Sub SortRangeVBA() once into a Personal Macro Workbook (or into the workbook itself), and run it each month via Developer ▸ Macros ▸ Run. The Sort a Range with VBA macro applies the same logic to every fresh dataset — consistent, repeatable, and immune to copy-paste slips. Use Sub SortRangeVBA() every time you need to sort a data range using a VBA macro reliably at scale. The macro eliminates the most common sources of error in manual work: missed rows, wrong column references, and inconsistent criteria. Once the code is tested and trusted, you can run it repeatedly with one click — zero cognitive overhead, zero variation in output. A practical tip before you scale it up: build it once on a small block of test data, confirm the number against the tool on this page, and only then point it at your real sheet. That one habit catches almost every mistake while it is still cheap to fix, long before a wrong figure reaches a report or a colleague.

In Google Sheets

Google Sheets handles this almost identically to Excel. The formula syntax above is the same, and the menu lives under a slightly different label rather than a ribbon tab. Use the platform toggle at the top of the page to switch every keyboard shortcut between Windows and Mac, and expect at most cosmetic differences in naming. Nothing on this page is behind a login: the tool runs entirely in your browser, the formula is shown in full with one-click copy, and the steps work the same on Windows and Mac. That is the whole promise here — the exact answer, a way to prove it on your own numbers, and just enough context to make it stick. Treat “excel vba sort range” as a small building block rather than a chore. Once the inputs sit in their own cells and the formula reads from them, the same setup answers a dozen related questions with a tweak, and Excel keeps every dependent figure current as the data changes. The tool above is there so you can rehearse and verify before committing anything to a real workbook; the steps and worked example are there so the logic sticks. Get it right once and it stops costing you time — it starts saving it, every time the question comes back around.

Common mistakes

  • Missing Option Explicit at the top of the module — adding it forces VBA to flag typos in variable names immediately, saving time debugging.
  • Not qualifying the range with the worksheet object — using Cells(1,1) without a ws. qualifier acts on whatever sheet is active; if the macro runs from a different sheet it may corrupt data. The pattern `Dim ws As Worksheet: Set ws = ActiveSheet` (or a specific sheet name) pins the action to the right place.
  • Forgetting that macros cannot be undone with Ctrl+Z once they have modified the sheet. Test on a copy of the workbook before running on live data.

Frequently asked questions

How do I run the Sort a Range with VBA macro?

Press Alt + F11 to open the VBA editor, insert a Module (Insert ▸ Module), paste Sub SortRangeVBA() into the code pane, and press F5. Alternatively close the editor and run it from Developer ▸ Macros ▸ select "SortRangeVBA" ▸ Run.

Why won't my macro run — macros are disabled?

Excel blocks macros by default for files downloaded from the internet. Click the yellow security bar and choose "Enable Content" — that trusts this one workbook and is all that is normally needed. Leave File ▸ Options ▸ Trust Center ▸ Trust Center Settings ▸ Macro Settings on "Disable VBA macros with notification" rather than switching to "Enable all macros", which Microsoft itself marks not recommended. If the bar is red instead of yellow, the file carries Mark of the Web: close Excel, right-click the file, open Properties and tick Unblock. Also ensure the file is saved as .xlsm, not .xlsx.

Can I undo what the Sort a Range with VBA macro did?

No — VBA actions generally cannot be undone with Ctrl+Z because they bypass Excel's undo stack. Always test Sub SortRangeVBA() on a backup copy of the workbook before running it on live data, or add explicit validation logic inside the macro before the destructive step.