VBA & Macro Problems

How to Create a VBA Macro to Sort an Excel Range by Column L

Aamir Naveed AkramAamir Naveed Akram Nov 2, 2026 870 views

Question details

The user needs to create a button-driven VBA macro to sort a specific Excel range (A6:L1000) in ascending order based on the values in Column L.

How to Create a VBA Macro to Sort an Excel Range by Column L
Product
Spreadsheet
Device & OS
not provided
Scenario
Automating data sorting processes within a large dataset using a clickable form control button.
Observed behavior
A functional VBA script that correctly targets the specified range and sorts it by the designated column upon clicking a mapped Form Control button.
Before you start

Ensure that the Developer tab is enabled in your spreadsheet application ribbon so you can access the Visual Basic Editor and insert Form Control buttons. Save your work before testing new macros to prevent unintended data loss.

Solution 1Recommended

Create and Assign a Button-Driven Sort Macro

Write a VBA script to sort the specified range and map it to a clickable button for easy execution.

By leveraging the Visual Basic Editor, you can define the exact range and sorting logic. Assigning this logic to a Form Control button provides a user-friendly way to execute the sort.

1
Open the Visual Basic Editor

Navigate to the Developer tab on your top ribbon and click on 'Visual Basic' to open the code editor.

2
Insert a New Module

In the Visual Basic Editor window, go to the top menu and select Insert > Module. This will create a blank canvas for your code.

3
Add the VBA Sorting Code

Paste the following script into the module: Sub SortRange() Range("A6:L1000").Sort Key1:=Range("L6"), Header:=xlYes End Sub. Note: Change 'xlYes' to 'xlNo' if row 6 is not a header row.

4
Insert a Form Control Button

Close the Visual Basic Editor and return to your spreadsheet. On the Developer tab, click 'Insert', select the 'Button' under Form Controls, and click anywhere on your sheet to draw it.

5
Assign the Macro to the Button

A prompt will appear as soon as you draw the button. Select 'SortRange' from the list of available macros and click OK.

6
Save as a Macro-Enabled File

To ensure your macro functions next time you open the file, go to File > Save As and select 'Excel Macro-Enabled Workbook (*.xlsm)'.

Create and Assign a Button-Driven Sort Macro
Test the Macro: Click the newly created button on your worksheet to instantly sort your A6:L1000 range. Ensure the document is saved as .xlsm so the macro is retained.
Automate Data Sorting with WPS Spreadsheet

Use WPS Office to Run and Manage VBA Macros

WPS Spreadsheet fully supports Excel VBA macros out of the box. You can seamlessly write, run, and assign VBA code to buttons to automate repetitive sorting tasks without paying for heavy enterprise software.

  1. 1. Open Spreadsheet: Launch WPS Spreadsheet and open your existing dataset.
  2. 2. Access VBA Editor: Navigate to the Developer tab and click on the 'Visual Basic' editor icon.
  3. 3. Write the Code: Insert a new module, paste your custom sorting script, and return to the main worksheet interface.
  4. 4. Insert and Assign Button: Use the Insert tool under the Developer tab to add a Form Control button and easily assign your new macro.
Fully supports running and editing Excel VBA and Macro scripts directly.Highly compatible with Microsoft Excel formats, including .xlsm files.Lightweight installation with a familiar, easy-to-use tabbed interface.Completely free to download and use for your daily spreadsheet automation tasks.
QA img-9

Frequently Asked Questions

Why is my VBA macro button not working after I reopen the file?

This usually happens if the file was saved as a standard .xlsx workbook, which strips out macros. You must save your document as a Macro-Enabled Workbook (.xlsm) for the VBA code to persist after closing the software.

How do I change the macro sorting order to descending?

Modify the Sort command in your VBA code by adding 'Order1:=xlDescending'. The updated snippet would look like this: Range("A6:L1000").Sort Key1:=Range("L6"), Order1:=xlDescending, Header:=xlYes.

Can I make the macro sort dynamically based on the last row of data instead of stopping at row 1000?

Yes, you can dynamically locate the last row by adding 'LastRow = Cells(Rows.Count, "L").End(xlUp).Row' before your sort command. Then, adjust your target range to use that variable: Range("A6:L" & LastRow).Sort.

How do I enable the Developer tab to access the Visual Basic Editor?

Go to File > Options > Customize Ribbon. Check the box next to 'Developer' in the right-hand pane, then click OK. The Developer tab will now appear on your main ribbon interface.