這裏有一個VBA程序重新格式化您的數據:
- 假定源數據是活動工作表,在
A1
開始
- 地方在
F1
主動片材起始導致 - 調整,以滿足您的需求
Sub SummariseList()
Dim rSrc As Range
Dim rDst As Range
Dim vSrc As Variant
Dim vDst() As Variant
Dim srcCol As Long, srcRow As Long
Dim dstCol As Long, dstRow As Long
Dim ID As Long
Dim i As Long
Set rSrc = Range([A1].End(xlToRight).Offset(1, 0), [A2].End(xlDown))
vSrc = rSrc
Set rDst = [F2]
' Count IDs and events '
dstRow = 1
dstCol = 1
i = 1
For srcRow = 2 To UBound(vSrc, 1)
If vSrc(srcRow, 1) = vSrc(srcRow - 1, 1) Then
i = i + 1
Else
dstRow = dstRow + 1
If dstCol < i Then dstCol = i
i = 1
End If
Next
If dstCol < i Then dstCol = i
ReDim vDst(1 To dstRow, 1 To dstCol + 1)
' Output table labels '
rDst.Offset(-1, 0) = "ID"
For i = 1 To dstCol
rDst.Offset(-1, i) = "Event_" & i
Next
' Create output data '
ID = vSrc(1, 1)
vDst(1, 1) = ID
dstRow = 1
dstCol = 2
For srcRow = 1 To UBound(vSrc, 1)
If vSrc(srcRow, 1) <> ID Then
' update vDst '
ID = vSrc(srcRow, 1)
dstCol = 2
dstRow = dstRow + 1
vDst(dstRow, 1) = ID
End If
For srcCol = 2 To UBound(vSrc, 2)
If vSrc(srcRow, srcCol) <> "" Then
vDst(dstRow, dstCol) = Chr(srcCol + 63)
dstCol = dstCol + 1
Exit For
End If
Next
Next
' Place result on sheet '
rDst.Resize(dstRow, dstCol - 1) = vDst
End Sub
出於某種原因,我得到「類型不匹配」的錯誤運行此宏。 – knarusk 2012-04-09 11:20:00
在哪一行?一種可能性,如果你的ID不是數字,則使用「Dim ID作爲Variant」而不是「Long」 – 2012-04-09 11:59:58