多个excel列成两列文件

时间:2017-06-01 10:20:53

标签: excel excel-formula

我有一个包含多列的excel文件,格式如下:

name_1      name_2  name_3  
name_1  name_4  name_5  name_6

这代表name_1与name_2和name_3合作,name_2与name_3等合作 所以我需要创建一个具有所有合作的新excel文件,例如

name_1  name_2
name_1  name_3
name_2  name_3
name_1  name_4
name_1  name_5
name_1  name_6
name_4  name_5
name_4  name_6
name_5  name_6

有没有办法做到这一点?

1 个答案:

答案 0 :(得分:0)

你需要VBA才能做到这一点。

如果您的数据是这样的,

enter image description here

试试以下代码,

Sub splitTable()
Dim i As Long, j As Long, k As Long, x As Long
Sheets.Add.Name = "newSheet"
k = 1
For i = 1 To Cells(Rows.Count, 1).End(xlUp).Row
    For x = 2 To Cells(i, Columns.Count).End(xlToLeft).Column
    For j = x To Cells(i, Columns.Count).End(xlToLeft).Column
        Sheets("newSheet").Cells(k, 1) = Cells(i, x - 1)
        Sheets("newSheet").Cells(k, 2) = Cells(i, j)
        k = k + 1
    Next j
    Next x
Next i
End Sub

代码创建一个新工作表并打印格式化的输出

enter image description here