Trying to copy the same range from all worksheets, and paste transposed into one worksheet. I want to get one line for each column in the destination worksheet. What I have tried so far looks like this:
Sub contracts()
Dim sh As Worksheet
Dim wb As Workbook
Dim DestSh As Worksheet
Dim DestShLastRow As Long
Dim i As Integer
Application.ScreenUpdating = False
Set wb = ActiveWorkbook
Set DestSh = wb.Sheets("Total table")
DestShLastRow = DestSh.Cells(DestSh.Rows.Count, "A").End(xlUp).Offset(1).Row
i = 1
For Each sh In ActiveWorkbook.Worksheets
If sh.Name = "Total table" Then Exit Sub
sh.Range("h3:h14").Copy
DestSh.Range ("a" & i)
.PasteSpecial xlPasteValues
.PasteSpecial Transpose = True
Application.CutCopyMode = False
End With
i = i + 1
Next
Application.ScreenUpdating = True
End Sub
When I run this code, I get an
1004-error, saying that "PasteSpecial method of Range class failed.
Anyone who has any advice on how to solve this problem?
With
in the code snippet (probably just a typo here). – BigBenIf sh.Name = "Total table" Then Exit Sub
withIf sh.Name = "Total table" Then Exit For
... If the sheet in discussion is not the last one, the code stops when it is reached. – FaneDuru.PasteSpecial Transpose = True
and moveTranspose
above, resulting:.PasteSpecial xlPasteValues, Transpose = True
. Of course, after addingWith
in front ofDestSh.Range ("a" & i)
... – FaneDuruDim arr As Variant
at the declarations area, thenarr = sh.Range("h3:h14").Value
followed by.Resize(ubound(arr,2), 1).Value = WorksheetFunction.Transpose(arr)
in theWith ... End With
area. – FaneDuru