VBA & Macro Problems

How to Sort Two Columns Using a Macro in LibreOffice and Excel

Anum MushtaqAnum Mushtaq Nov 11, 2026 868 views

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.

How to Sort Two Columns 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.
Before you start

Ensure your spreadsheet headers are correctly positioned on row 3 and that your macro security settings allow the execution of VBA or Basic scripts.

Solution 1Recommended

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.

1
Open the Macro Editor

Navigate to the Developer tab and open the Macro Editor, or use the appropriate shortcut (such as Alt + F11 in Excel).

2
Insert the Macro Code

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.

3
Create a Macro Button

Go to your 'Grid_References' sheet, navigate to the Developer tab, click on Insert, and place a button on your spreadsheet.

4
Assign the Macro

Right-click the newly created button, select 'Assign Macro', and choose the Grid_References macro. Clicking this button will now trigger the sorting automatically.

Using a Macro Module to Sort Paired Columns
Excel Compatibility: Standard sorting macros written in this format often work interchangeably between LibreOffice and Excel without any further code modification.
Automate Your Spreadsheets

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. 1. Install WPS Office: Download and install WPS Office for free from the official website.
  2. 2. Open Your Spreadsheet: Launch WPS Spreadsheet and open your .xlsm or .xlsx file containing the address and grid-reference data.
  3. 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. 4. Execute the Macro: Run the macro or assign it to a form button on your sheet to sort your paired columns instantly.
Advanced VBA and macro support for complex task automation.Seamlessly compatible with Microsoft Excel (.xlsx, .xls, .xlsm) formats.Built-in robust sorting, filtering, and data organization tools.Lightweight software with a familiar, easy-to-use interface.
microsoft office alternative - wps office

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.