2012-04-09 2 views
0

I가이정렬 및 그룹화 행

ID Event_A Event_B Event_C 
1 01-01 
1 01-02 
1   01-05 
2 01-02 
2     01-03 
3 01-03 
3 01-04 
3   01-06 
3     01-08 

처럼 보이는 내가

그래서 기본적으로 각 ID에 대해 하나 개의 행이이

ID Event_1 Event_2 Event_3 Event_4 
1 A  A  B 
2 A  C 
3 A  A  B  C 
같은 출력을 필요로 테이블 및 시간 소인 (연대순)에 따라 열의 모든 이벤트 유형.

누구든지 매크로를 OR SQL 쿼리와 같이 원래 테이블을 돌리는 방법을 조언 할 수 있습니까?

답변

0

여기에 데이터 포맷하는 VBA 루틴입니다 :

  1. 가 소스 데이터가
  2. 장소는 F1에서 활성 시트의 시작에 결과 A1에서 시작, 현재 시트에 가정 - 사용자의 요구에 맞게 조정을
내가 이것을 실행하기위한 '형식 불일치'오류가 몇 가지 이유를 들어
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 
+0

매크로. – knarusk

+0

어떤 행에 있습니까? 한 가지 가능성은 ID의 숫자가 아닐 경우 'Long ID'대신 'Dim ID as Variant'를 사용하는 것입니다. –