如何在Excel中“旋转”数据(复杂转置)
说我有这个信息:
NAME Class1 Class2 Class3 NAME2 Class2 Class3 Class4
我想把它变成:
NAME Class1 NAME Class2 NAME Class3 NAME2 Class2 NAME2 Class3 NAME2 Class4
我会使用粘贴>移调,除了我有550个名称,每个最多10个类。 所以我不得不复制,插入10空行,粘贴>转置,然后修剪空行500次。
有更好的方法吗?
在Sheet1中有这样的数据
运行这个macros:
Sub ReOrganize() Dim sh1 As Worksheet, sh2 As Worksheet Dim N As Long, i As Long, j As Long, k As Long Dim M As Long, v As String Set sh1 = Sheets("Sheet1") Set sh2 = Sheets("Sheet2") k = 1 N = sh1.Cells(Rows.Count, "A").End(xlUp).Row For i = 1 To N v = Cells(i, 1).Value M = Cells(i, Columns.Count).End(xlToLeft).Column For j = 2 To M sh2.Cells(k, 1) = v sh2.Cells(k, 2) = sh1.Cells(i, j) k = k + 1 Next j Next i End Sub
将在Sheet2中产生这个
上面的答案看起来像一个我最终在https://superuser.com/questions/633124/how-do-i-split-one-row-into-multiple-rows-with-excel
我从该页面使用的代码本身是:
Sub NewLayout() For i = 2 To Cells.Find("*", [A1], , , xlByRows, xlPrevious).Row For j = 0 To 2 If Cells(i, 3 + j) <> vbNullString Then intCount = intCount + 1 Cells(i, 1).Copy Destination:=Cells(intCount, 10) Cells(i, 2).Copy Destination:=Cells(intCount, 11) Cells(i, 3 + j).Copy Destination:=Cells(intCount, 12) End If Next j Next i End Sub
而不是像上面的答案粘贴到新的工作表,粘贴到同一工作表的第10-12列。