Transpose from rows to column (VBA)

风流意气都作罢 提交于 2020-01-07 04:07:09

问题


My question is: how to transpose the last four rows of the column G into different columns?

I usually use this static code:

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

But it doesn't allow me to stay on the same Sheet.

So I am trying to combine it with this code:

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

Before

After

Any help is appreciated


回答1:


Untested:

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


来源:https://stackoverflow.com/questions/38507901/transpose-from-rows-to-column-vba

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