我有一组复杂的数据需要在 Excel 中重新排列。
表格看起来像这样
+----+--------+--------+--------+--------+
| ID | Text A | Text B | Text C | Text D |
+----+--------+--------+--------+--------+
| 1 | xx | | yy | zz |
+----+--------+--------+--------+--------+
| 2 | | aa | bb | |
+----+--------+--------+--------+--------+
| 3 | cc | | | dd |
+----+--------+--------+--------+--------+
我需要输出的结果本质上是转置和合并的组合。但我不知道如何在 Excel 中解决这个问题。
+----+------+
| ID | Text |
+----+------+
| 1 | xx |
+----+------+
| 1 | yy |
+----+------+
| 1 | zz |
+----+------+
| 2 | aa |
+----+------+
| 2 | bb |
+----+------+
| 3 | cc |
+----+------+
| 3 | dd |
+----+------+
答案1
假设原始数据为工作表1输出将被放置在工作表2。
运行这个简短的宏:
Sub ReOrganizer()
Dim N As Long, nCols As Long
Dim i As Long, j As Long, k As Long
Dim s1 As Worksheet, s2 As Worksheet
Set s1 = Sheets("Sheet1")
Set s2 = Sheets("Sheet2")
N = s1.Cells(Rows.Count, 1).End(xlUp).Row
nCols = 5
k = 2
For i = 2 To N
Item = s1.Cells(i, 1)
For j = 2 To nCols
If s1.Cells(i, j) <> "" Then
s2.Cells(k, 1) = Item
s2.Cells(k, 2) = s1.Cells(i, j)
k = k + 1
End If
Next j
Next i
End Sub
将获取如下数据工作表1:
并制作这个工作表2: