In Excel: paste Sub SortRangeVBA() into the VBA editor (Alt + F11 → Insert ▸ Module), press F5 to run, and save as .xlsm to keep the macro. Excel for the web cannot create, run, or edit VBA macros. Open the workbook in the Excel desktop app to work with macros.
On this page8
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 SubKey1 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
- Press Alt + F11 to open the VBA editor.
- Insert > Module.
- Paste the code above.
- Press
F5, or close the editor and run it from Developer > Macros. - Save the file as .xlsm so the macro is kept.
Open Excel and navigate to Developer ▸ Visual Basic (or press Alt + F11).
Select Insert ▸ Module from the menu bar inside the VBA editor.
Copy and paste Sub SortRangeVBA() into the Module.
Click anywhere inside the Sub body and press F5 to run.
Close the VBA editor and save the file as .xlsm.
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. A macro runs in the desktop app's VBA environment, not in the cell grid: the workbook has to be saved in a macro-enabled format such as .xlsm to keep the code, and your macro security settings — or a file that arrived from the internet — can block it until you trust it. Excel for the web cannot run VBA at all. 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. Run a new macro on a copy of the workbook first. Do not count on Undo to reverse what a macro changed; the saved copy is your way back.
In Google Sheets
Google Sheets does not run VBA. A macro written for Excel has to be rewritten in Google Apps Script (Extensions ▸ Apps Script), and an .xlsm file converted to Sheets keeps its cells but drops the code. Nothing on this page is behind a login or a download: the exact answer is at the top, and the detail below it is there for when you need it. That is the whole promise here — the answer first, and just enough context to make it stick. Treat “excel vba sort range” as code you are responsible for: know what it changes, keep a copy of the workbook before running it and keep the file in a macro-enabled format. In Google Sheets and Excel for the web this code does not run; those need a different mechanism.
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.