How to Transpose Variable-Length Data Sets Using an Excel VBA Macro
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.

- 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 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.
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.
Open your Excel workbook and press ALT + F11 to launch the Visual Basic for Applications (VBA) Editor.
In the VBA Editor, click on 'Insert' in the top menu bar, then select 'Module' to create a blank script window.
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
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 Power Query to Transpose Data (Alternative for Excel 365)
For users with modern Excel versions, Power Query offers a powerful, code-free method to transpose columns with variable-sized data blocks using data grouping and pivoting.
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. Open Your Data: Launch WPS Spreadsheet and open the file containing the vertical data sets.
- 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. Insert the Transpose Script: Insert a new module and paste the provided VBA transpose code into the window.
- 4. Execute the Macro: Save your code, return to your worksheet, and run the macro to instantly reshape your vertical data into horizontal rows.

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.




