如何将 Excel 中的一行数据拆分为多行?
鉴于此输入:
Name Award Type 1 Amount 1 Award Type 2 Amount 2
Dan Top Performer $5,000 Top Region $10,000
Kathy Rep Planmaker $2,500
我想将其转换为这样:
Name Award Type Amount
Dan Top Performer $5,000
Dan Top Region $10,000
Kathy Rep Planmaker $2,500
换句话说:由于输入数据中的“Dan”行显示两个奖项 - 一个在 B 和 C 列,另一个在 D 和 E 列 - 我想将 Dan 的数据分成两行,每个奖项一行。
答案1
Sub convert()
Dim intcount As Integer
intcount = 1
For i = 2 To **123**
For j = 0 To 1
If ThisWorkbook.Sheets("sheet1").Cells(i, 1 + 2 * j).Value <> vbNullString Then
intcount = intcount + 1
ThisWorkbook.Sheets("sheet1").Cells(i, 1).Copy Destination:=ThisWorkbook.Sheets("sheet2").Cells(intcount, 1)
ThisWorkbook.Sheets("sheet1").Cells(i, 2 + 2 * j).Copy Destination:=ThisWorkbook.Sheets("sheet2").Cells(intcount, 2)
ThisWorkbook.Sheets("sheet1").Cells(i, 3 + 2 * j).Copy Destination:=ThisWorkbook.Sheets("sheet2").Cells(intcount, 3)
End If
Next
Next
End Sub