I'm trying to something very simple, but I seem to be stuck. I am following the help menu for PasteSpecial
but I cannot seem to get my code to work without an error.
I want to take Worksheets("Sheet1").Range("A1","A5")
and paste transpose to Worksheets("Sheet2").Range("A1","E1")
.
What is the most simple way to accomplish this?
Follow the below steps to use Transpose in VBA. Step 1: Insert a new module and define a new sub-procedure to create a macro in VBA. Step 2: Define a new variable which can hold your one-dimensional array. Step 3: Define the list as an array using the Array function.
Then press Ctrl + C to copy the selected cells, and select a cell to right click to open the context menu, and click Paste Special > Transpose to paste the cells in transposition.
Worksheets("Sheet1").Range("A1:A5").Copy
Worksheets("Sheet2").Range("A1").PasteSpecial Transpose:=True
Here's an efficient option that doesn't use the clipboard.
Sub transposeAndPasteRow(rowToCopy As Range, pasteTarget As Range)
pasteTarget.Resize(rowToCopy.Columns.Count) = Application.WorksheetFunction.Transpose(rowToCopy.Value)
End Sub
Use it like this.
Sub test()
Call transposeAndPasteRow(Worksheets("Sheet1").Range("A1:A5"), Worksheets("Sheet2").Range("A1"))
End Sub
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With