在循环中转置静态范围



我试图循环地将工作表 1 上的 A 列到工作表 3 上的行的静态范围转置,但无济于事。这是我到目前为止使用的代码:

Sub Looptranspose()
'
' Looptranspose Macro
'
' Keyboard Shortcut: Ctrl+a
    Dim x As Integer
    Dim y As Integer
    x = 1
    y = x + 18
    Range("A" & CStr(x) & ":A" & CStr(y)).Select
    Selection.Copy
    Sheets("Sheet3").Select
    Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
        False, Transpose:=True
    x = x + 19
End Sub

在 A 行完成之前如何循环?此代码将A列中选定的19个单元格的第一个范围转置为工作表3上的选定行。

我需要宏来选择工作表 19 中 A 行中的下一个单元格,然后转置到工作表 3 的下一行。下面是示例:

Excel 步骤 1https://drive.google.com/file/d/0B2TQdtpfUIa5OUpRTWNwLUQ5WVk/edit?usp=sharing

Excel 步骤 2https://drive.google.com/file/d/0B2TQdtpfUIa5TkNrVXRwOHh2TFk/edit?usp=sharing

如何继续选择工作表A行19中的以下单元格(直到没有更多数据)并转置到工作表3中的以下行?

查看您的代码和屏幕截图,我认为您想在一张纸中获取一长列 A,并将其以 19 个单元格块转置到另一张纸中。

问题是 - 您实际上没有包含循环,并且没有更新目的地的位置。我尝试在我的示例中修复这些东西。如果这不是您想要的,请发表评论。

注意 - 通常使用 .Select 会使代码变慢、难以阅读且容易出错。最好创建引用特定范围的对象。

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

最新更新