How to Sort Two Columns Using a Macro in LibreOffice and Excel
Question details
The user needs to sort address and grid-reference data in columns A and B by column A, keeping the paired data intact using a macro.

- Product
- LibreOffice / Excel
- Device & OS
- not provided
- Scenario
- Organizing paired data (like addresses and grid references) with headers located on row 3.
- Observed behavior
- The goal is to automatically sort the data using a macro button while preserving the relationship between the data in the two columns.
Ensure your spreadsheet headers are correctly positioned on row 3 and that your macro security settings allow the execution of VBA or Basic scripts.
Using a Macro Module to Sort Paired Columns
Create and assign a macro to sort columns A and B simultaneously based on the values in column A.
When sorting paired data, it is crucial to expand the sort selection to include both columns so the rows remain linked. The macro automates this process without needing manual selection every time.
Navigate to the Developer tab and open the Macro Editor, or use the appropriate shortcut (such as Alt + F11 in Excel).
Insert a new Module and paste your sorting code. Ensure it starts directly with Sub Grid_References() and ends with End Sub, removing any preceding lines or extra declarations above it.
Go to your 'Grid_References' sheet, navigate to the Developer tab, click on Insert, and place a button on your spreadsheet.
Right-click the newly created button, select 'Assign Macro', and choose the Grid_References macro. Clicking this button will now trigger the sorting automatically.

Sort Data and Run Macros Easily with WPS Spreadsheet
WPS Spreadsheet offers comprehensive support for VBA macros, allowing you to easily automate tasks like sorting paired columns. Enjoy a highly compatible and lightweight spreadsheet environment designed to boost your productivity.
- 1. Install WPS Office: Download and install WPS Office for free from the official website.
- 2. Open Your Spreadsheet: Launch WPS Spreadsheet and open your .xlsm or .xlsx file containing the address and grid-reference data.
- 3. Access the VBA Editor: Go to the Developer tab, open the VBA Editor, and paste your sorting macro directly into a new module.
- 4. Execute the Macro: Run the macro or assign it to a form button on your sheet to sort your paired columns instantly.

Frequently Asked Questions
Why are my rows getting mixed up when I sort columns?
This happens if you only select one column when sorting. Always ensure that the sorting range includes all paired columns so the data in adjacent cells stays together.
How do I enable macros if the Developer tab is missing?
You can enable the Developer tab by going to File > Options > Customize Ribbon, and checking the box next to 'Developer'. This allows you to access macro settings and the VBA editor.
Will an Excel sorting macro work in LibreOffice?
Basic sorting macros often work across both platforms if LibreOffice is set to support VBA properties. However, you may occasionally need to adjust specific API calls depending on the complexity of the macro.




