需要将多列转换为行

时间:2016-08-15 21:44:57

标签: excel rows

我有一个看起来像这样的数据集

Host, software 1, software 2. software 3......

我需要它看起来像这样

host,software 1
host,software 2
host,software 3

对此的任何提示或建议都将非常感激。

由于

1 个答案:

答案 0 :(得分:0)

你熟悉VBA吗?如果是这样,试试这个。只需将其粘贴到模块中即可运行。我确信这不是最有效的方式,但它会完成工作。

我还假设您只有3个软件列。如果你有更多,你需要改变“为j = 1到3”的部分来说'对于j = 1到X',其中X是你拥有的列数。

Sub transfer()

'Please make sure to change the name of the sheets to reflect the _
 correct worksheet names.
Dim wkInput As Worksheet: Set wkInput = Worksheets("sheet1")
Dim wkDest As Worksheet: Set wkDest = Worksheets("sheet2")

'Here we find the maximum number of rows that will be transfered.
Dim nl As Integer: nl = wkInput.UsedRange.Rows.Count

'Here we create a few couner variables.
Dim Row As Integer, Col As Integer, i As Integer, j As Integer

'*********Procedure begins here************
Row = 1
Col = 1
For j = 1 To 3
    For i = 1 To nl - 1
        wkDest.Cells(Row, Col).Value = wkInput.Cells(i + 1, 1).Value
        wkDest.Cells(Row, Col + 1).Value = wkInput.Cells(i + 1, j + 1).Value
        Row = Row + 1
    Next i
Next j

'*******Procedure ends here*************
End Sub
相关问题