How to Create a VBA Macro to Sort an Excel Range by Column L
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.

- 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.
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.
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.
Navigate to the Developer tab on your top ribbon and click on 'Visual Basic' to open the code editor.
In the Visual Basic Editor window, go to the top menu and select Insert > Module. This will create a blank canvas for your 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.
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.
A prompt will appear as soon as you draw the button. Select 'SortRange' from the list of available macros and click OK.
To ensure your macro functions next time you open the file, go to File > Save As and select 'Excel Macro-Enabled Workbook (*.xlsm)'.

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. Open Spreadsheet: Launch WPS Spreadsheet and open your existing dataset.
- 2. Access VBA Editor: Navigate to the Developer tab and click on the 'Visual Basic' editor icon.
- 3. Write the Code: Insert a new module, paste your custom sorting script, and return to the main worksheet interface.
- 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.

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.




