How to Get Excel VBA Row and Column Header Values
Question details
The user wants to use a VBA macro to retrieve the row and column header values corresponding to the currently active cell.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- Writing an Excel VBA macro that dynamically identifies headers based on user cell selection instead of using a static, hardcoded cell address.
- Observed behavior
- The goal is to programmatically extract the text from the top row and first column that correspond to the active cell's location and display or use those values.
Ensure that the Developer tab is enabled in your Excel ribbon, and verify the exact row and column indexes where your table headers are stored (usually Row 1 and Column A).
Use ActiveCell Properties to Retrieve Headers
Use a VBA script to dynamically fetch the header values based on the ActiveCell's current row and column numbers.
By referencing the 'ActiveCell.Row' and 'ActiveCell.Column' properties, VBA can pinpoint the exact position of your selected cell. Assuming your column headers are placed in Row 1 and your row headers are in Column A, the 'Cells' property can extract their exact text values dynamically.
Launch your Excel workbook and press 'ALT + F11' on your keyboard to open the Visual Basic for Applications (VBA) Editor.
In the top menu, click on 'Insert' and select 'Module' to create a new blank script window.
Copy and paste the following code: Sub GetHeaders() Dim c As Range Set c = ActiveCell MsgBox "Column header: " & Cells(1, c.Column).Value & vbCrLf & "Row header: " & Cells(c.Row, 1).Value End Sub
Return to your worksheet, click on any data cell inside your table, and run the macro (via 'ALT + F8' or a custom button). A message box will immediately display the respective row and column headers.

Run VBA Macros Seamlessly in WPS Office
WPS Spreadsheets provides robust support for VBA and macros. You can run the exact same VBA script to dynamically retrieve row and column headers in WPS Office without modifying any code.
- 1. Open WPS Spreadsheets: Launch WPS Office and open your workbook containing the data table.
- 2. Access the Developer Tools: Navigate to the 'Developer' tab in the top ribbon and click on 'VBA Editor' (or press ALT + F11).
- 3. Paste the VBA Code: Insert a new module within the editor and paste the GetHeaders macro code.
- 4. Execute the Macro: Select a cell in your spreadsheet and run the macro to instantly extract its associated headers.

Frequently Asked Questions
How do I reference headers on a different worksheet using VBA?
To reference headers on another sheet safely, use fully qualified worksheet references. For example, instead of just using 'Cells(1, c.Column)', specify the sheet by using 'Worksheets("SheetName").Cells(1, c.Column)'.
Why does the VBA macro return a blank value for the header?
This happens if the header row or column you referenced in the code (such as Row 1 or Column A) is actually empty. Ensure the macro's row and column index numbers exactly match where your table headers are placed.
Can I output the header values to another cell instead of a message box?
Yes. You can assign the retrieved header value directly to a target cell. For example, use 'Range("D1").Value = Cells(1, c.Column).Value' to write the column header into cell D1.




