简体   繁体   English

Excel VBA:将列转置为行

[英]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. 在这种情况下,我希望列在第5行转置,而不是像第二张图片中看到的那样跳到第1行。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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