简体   繁体   中英

Excel VBA: Transpose a column to a row

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM