Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel VBA - Range.Copy transpose paste

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?

like image 746
user1130306 Avatar asked Jan 13 '12 15:01

user1130306


People also ask

How do you use the Transpose function in VBA?

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.

How do you copy and paste transpose formulas?

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.


2 Answers

Worksheets("Sheet1").Range("A1:A5").Copy
Worksheets("Sheet2").Range("A1").PasteSpecial Transpose:=True
like image 89
GSerg Avatar answered Oct 20 '22 21:10

GSerg


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
like image 44
GollyJer Avatar answered Oct 20 '22 21:10

GollyJer