VBA & Macro Problems

How to Transpose Variable-Length Data Sets Using an Excel VBA Macro

Anum MushtaqAnum Mushtaq Nov 3, 2026 868 views

Question details

The user needs a method to copy vertical data blocks separated by blank rows and transpose each block horizontally, particularly when the number of rows differs between data sets.

How to Transpose Variable-Length Data Sets with an Excel VBA Macro
Product
Excel
Device & OS
not provided
Scenario
Restructuring and organizing datasets where groups of data are stored sequentially in a single column with blank rows acting as separators.
Observed behavior
Data is currently formatted as vertical blocks of varying lengths and must be pivoted into corresponding horizontal rows.
Before you start

Before running the macro, ensure you have enabled the Developer tab in your ribbon menu and verify that your vertical data blocks are separated by exactly one blank row.

Solution 1Recommended

Use a VBA Macro to Transpose the Data Blocks

This VBA script iterates through a specific column, identifying blank rows to determine block boundaries, and transposes each variable-length block horizontally next to its starting row.

This automated macro assumes your source data is located in Column A starting from row 2, and headers occupy row 1. It dynamically loops through the cells, pivoting data to adjacent columns whenever it detects a blank row separator.

1
Launch the VBA Editor

Open your Excel workbook and press ALT + F11 to launch the Visual Basic for Applications (VBA) Editor.

2
Insert a New Module

In the VBA Editor, click on 'Insert' in the top menu bar, then select 'Module' to create a blank script window.

3
Paste the Transpose Code

Copy and paste the following VBA code into the module window: Sub TransposeRanges() Dim StartRow As Long Dim EndRow As Long Dim k As Long Dim x As Integer, y As Integer StartRow = 2 EndRow = Cells(Rows.Count, "A").End(xlUp).Row x = StartRow y = 1 For k = StartRow To EndRow If Not IsEmpty(Cells(k, "A")) Then Cells(x, "A").Offset(0, y).Value = Cells(k, "A").Value y = y + 1 Else x = k + 1 y = 1 End If Next k End Sub

4
Run the Macro

Close the VBA Editor to return to your worksheet. Press ALT + F8 to open the Macro dialog box, select 'TransposeRanges' from the list, and click 'Run'.

Use a VBA Macro to Transpose the Data Blocks
Adjusting the Source Range: If your source layout differs, adjust the macro variables. Change 'StartRow = 2' to your actual starting row, and update references to '"A"' if your data resides in a different column.
Powerful Spreadsheet Tool

Easily Manage Macros and Transpose Data with WPS Spreadsheet

WPS Spreadsheet fully supports VBA macros, allowing you to run complex data transformations such as transposing variable-length data sets just as seamlessly as you would in Microsoft Excel.

  1. 1. Open Your Data: Launch WPS Spreadsheet and open the file containing the vertical data sets.
  2. 2. Access the Developer Tab: Navigate to the 'Developer' tab on the ribbon and click the 'Macro' or 'VBA Editor' button to access the scripting environment.
  3. 3. Insert the Transpose Script: Insert a new module and paste the provided VBA transpose code into the window.
  4. 4. Execute the Macro: Save your code, return to your worksheet, and run the macro to instantly reshape your vertical data into horizontal rows.
Fully compatible with Microsoft Excel (.xlsx and .xlsm) macro-enabled formats.Built-in VBA editor makes it easy to create, edit, and run scripts.Lightweight, fast, and completely free to download.Familiar spreadsheet interface ensures a zero-learning-curve transition.
microsoft office alternative - wps office

Frequently Asked Questions

Can I change the destination column for the transposed data in the macro?

Yes. The current VBA script uses the 'Offset(0, y)' function to paste data horizontally starting adjacent to the original data block. By modifying the base reference or offset values, you can redirect the transposed output to a completely different column or worksheet.

Why does my macro stop transposing at a certain row?

The macro logic relies on blank rows (IsEmpty) to determine where one data block ends and another begins. If there are consecutive blank rows or unexpected hidden characters, the script might misinterpret the data structure. Ensure there is strictly one blank row between each data set.

How do I save a file containing this VBA macro?

Workbooks containing active macro scripts must be saved as a Macro-Enabled Workbook. Go to Save As, and choose the .xlsm format from the file type dropdown menu to ensure your macro code is saved for future use.