How to Transpose Data and Repeat Headings Using Excel VBA
Question details
The user needs a method to copy an entire data region along with its headings and paste it into a new worksheet as transposed values using an automated Excel VBA script.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Automating the transposition of a dataset (converting rows to columns or vice versa) including its headers to a fresh worksheet for better data organization and layout.
- Observed behavior
- The data is successfully copied from the source sheet, transposed with values only, pasted onto a newly created worksheet, and the column widths are automatically adjusted to fit the new content layout.
Ensure that your workbook is saved as a Macro-Enabled Workbook (.xlsm) to prevent losing your VBA code, and confirm that the Developer tab is visible in your Excel ribbon.
Use a VBA Macro to Transpose Data to a New Worksheet
Run a VBA script that copies the current data region, creates a new sheet, and pastes the data transposed while automatically fitting the columns.
The provided macro uses the CurrentRegion property to automatically select all contiguous data starting from cell A1. It then utilizes PasteSpecial with the Transpose:=True argument to flip the rows and columns on a newly generated worksheet.
Press ALT + F11 on your keyboard to open the Visual Basic for Applications (VBA) editor.
Click 'Insert' from the top menu and select 'Module' to create a blank module window where the script will reside.
Copy and paste the following code into the module window: Sub CopyPaste_ValuesTranspose() Application.ScreenUpdating = False Dim ws1 As Worksheet, ws2 As Worksheet Set ws1 = Sheets("Sheet1") Set ws2 = Sheets.Add(After:=Sheets(Sheets.Count)) ws1.Range("A1").CurrentRegion.Copy ws2.Range("A1").PasteSpecial xlValues, Transpose:=True Application.CutCopyMode = False ws2.Cells.EntireColumn.AutoFit Application.ScreenUpdating = True End Sub
Close the VBA editor and return to Excel. Press ALT + F8 to open the Macro dialog box, select 'CopyPaste_ValuesTranspose' from the list, and click 'Run' to execute the transposition.

Transpose Data Manually Using Paste Special
If you prefer a code-free approach or only need to perform the action once, you can quickly transpose data manually using the built-in Paste Special feature.
Easily Transpose Data and Run Macros in WPS Spreadsheet
WPS Spreadsheet fully supports VBA macros, allowing you to seamlessly run your transpose scripts just like in Excel. It offers a familiar interface, ensuring you can manage data transformations quickly and efficiently.
- 1. Open Your Workbook: Launch WPS Spreadsheet and open the data file you need to transpose.
- 2. Access the VBA Editor: Navigate to the 'Developer' tab and click on 'VB Editor' to input and save your macro code.
- 3. Execute the Transposition: Run the macro to automatically create a new transposed sheet, or use the right-click menu to Paste Special > Transpose for manual adjustments.

Frequently Asked Questions
Why am I getting a 'Subscript out of range' error when running the transpose macro?
This usually happens if the source sheet name in the code does not match the actual name of your worksheet. Update the `Set ws1 = Sheets("Sheet1")` line in the VBA editor to exactly match your current tab name.
Does the macro copy formulas or just the data values?
The macro provided uses `xlValues`, meaning it will convert any formulas from the source data into static text and numbers in the transposed sheet. If you need to keep formulas intact, change `xlValues` to `xlPasteAll` in the code.
How do I make the macro dynamic so it works on any sheet I have open?
You can modify the code by replacing `Set ws1 = Sheets("Sheet1")` with `Set ws1 = ActiveSheet`. This allows the macro to extract data from whichever worksheet you are actively viewing when you run the code.
Can I assign this transpose macro to a clickable button?
Yes. Go to the Developer tab, insert a Button (Form Control), draw it on your spreadsheet, and when the 'Assign Macro' dialog appears, select your 'CopyPaste_ValuesTranspose' macro.




