Excel VBA: Transpose a column to a row

寵の児 提交于 2019-12-13 17:15:31

问题


I need to transpose my Column to a Row. I have a code I found that works but it is not doing exactly what I desire.

This is what my file looks like before I run the code:

This is what my file looks like after I run the code:

I want my result to be displayed on the row that the top cell of the column is on. In this case, I want the column to transpose on the 5th row instead of jumping up to the 1st like you see in the second picture.

This is my Code:

Private Sub CommandButton1_Click()
Dim rng As Range
Dim I As Long

    Set rng = Range("B5")
    While rng.Value <> ""
        I = I + 1
        rng.Resize(60).Copy
        Range("C" & I).PasteSpecial Transpose:=True
        Set rng = rng.Offset(60)

    Wend
    rng.EntireColumn.Delete
End Sub

回答1:


One way:

'// source column range
Set rng = Range("B5", Range("B5").End(xlDown))

'// resize from destination cell, transpose
Range("B5").Resize(rng.Columns.Count, rng.Rows.Count).Value = WorksheetFunction.Transpose(rng)

'// clear transposed values
'// offset by 1 to preserve B5
rng.Offset(1, 0).Clear


来源:https://stackoverflow.com/questions/39980532/excel-vba-transpose-a-column-to-a-row

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