Transpose static range in a loop

非 Y 不嫁゛ 提交于 2019-12-02 13:33:07

Looking at your code and the screen shots, I think you want to take a long column A in one sheet, and transpose it in 19-cell chunks into another sheet.

The problem is - you don't actually include a loop, and you don't update the location of the destination. I tried fixing those things in my example. If that is not what you wanted, please leave a comment.

Note - typically using .Select will make your code slow, hard to read, and prone to errors. It is better to create objects that reference specific ranges.

Sub copyChunk()
' copy chunks of 19 cells from column A of sheet 1
' and paste their transpose on sheet 3
' starting in the first row

Dim sh1 As Worksheet, sh2 As Worksheet
Dim r1 As Range, r2 As Range
Dim chunk As Integer
chunk = 19

Set sh1 = ActiveWorkbook.Sheets("sheet1")
Set sh2 = ActiveWorkbook.Sheets("sheet3")

' picking the starting point here - this could be "anywhere"
Set r1 = Range(sh1.Cells(1, 1), sh1.Cells(chunk, 1))
Set r2 = sh2.[A1]

While Application.WorksheetFunction.CountA(r1) > 0
  r1.Copy
  r2.PasteSpecial Paste:=xlPasteAll, SkipBlanks:=False, Transpose:=True
  ' move down "chunk" cells for the source
  Set r1 = r1.Offset(chunk, 0)
  ' move down one row for the destination
  Set r2 = r2.Offset(1, 0)
Wend

End Sub
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!