|
发表于 2012-11-21 10:14
|
显示全部楼层
本楼为最佳答案
1,将源数据按现在格式,复制到结果文件中,工作表命名为 "源数据"
2,将结果工作表,命名为“结果"
当然你可以将上述名称改变,代码中作相应改动
Sub MyTranspose()
Dim Sh As Worksheet
Set Sh = Sheets("源数据")
With Sheets("结果")
.Range("a4").Resize(.Rows.Count - 3, .Columns.Count).ClearContents
Sh.Columns("a:b").Sort Sh.Range("a2"), Header:=xlYes
j = 3
For i = 2 To Sh.Range("a" & Rows.Count).End(3).Row
If Sh.Cells(i, 1) <> Sh.Cells(i - 1, 1) Then
j = j + 1
col = 1
.Cells(j, col) = Sh.Cells(i, 1)
End If
col = col + 1
.Cells(j, col) = Sh.Cells(i, 2)
Next i
End With
End Sub
|
|