从行到列的转置(VBA)

时间:2016-07-21 15:07:52

标签: vba excel-vba excel

我的问题是:如何将G列的最后四行转换为不同的列?

我通常使用这个静态代码:

Worksheets("Sheet8").Range("A1:A5").Copy
Worksheets("Sheet9").Range("A1").PasteSpecial Transpose:=True

但它不允许我留在同一张纸上。

所以我想把它与这段代码结合起来:

Dim r As Range, N As Long
N = Cells(Rows.Count, "A").End(xlUp).Row
Set r = Cells(N, 1).EntireRow

r.Copy
Cells(N + 1, 1).PasteSpecial Transpose:=True
r.Delete

enter image description here

之后

enter image description here

感谢任何帮助

1 个答案:

答案 0 :(得分:1)

未测试:

Dim c As Range, v

'find last-used cell in ColG
Set c = Cells(Rows.Count, "G").End(xlUp)

With c.offset(-3,0) 'starting with the cell 3 rows above the last-used cell...
    v = .resize(4,1).value      'get the value of the 4-row/1-col range below
    .resize(4,1).clearcontents  '...then clear that range
    .resize(1,4).value = Application.Transpose(v) 'place the values in a row
End with