2011-06-03 44 views
1

我需要一些幫助,使用Excel宏將三個柱轉換爲矩陣。 下面是一個例子:如何使用宏將三列轉換爲矩陣

從這:

A A 0 
A B 23 
A C 3 
B A 7 
B B 56 
B C 33 
C A 31 
C B 6 
C C 5

這樣:

 A B C 
A 0 23 3 
B 7 56 33 
C 31 6 5

希望你能幫助我。
謝謝

+0

特別使用vba? Excel樞軸不是一個選項? – GSerg 2011-06-03 20:55:35

回答

0

不太清楚你到底是什麼意思的矩陣。對於下面的代碼,我假設您正在尋找一種方法來讀取前兩列中的數據,作爲輸出表的Row和Column數據。假設輸入數據位於「Sheet1」的列1 - 3中

Sub ConvertTableOfData() 
Dim testArray(1 to 3) 
Dim chkROW as Integer 
Dim chkCOL as Integer 
Dim chkVAL as Integer 

'// index the Row and Column headers 
testArray(1) = "A" 
testArray(2) = "B" 
testArray(3) = "C" 

'// Iterate through every row in the initial dataset 
For i = 1 to Worksheets("Sheet1").Cells(1, 1).End(xlDown).Row 

    With Worksheets("Sheet1") 
     '// Assign the Output Row and Column values 
     '// based on the array indices 
     For j = 1 to UBound(testArray, 1) 
      If .Cells(i, 1) = testArray(j) Then 
       chkROW = j 
      End If 
      If .Cells(i, 2) = testArray(j) Then 
       chkCOL = j 
      End If 
     Next j 

     '// store the actual value 
     chkVAL = .Cells(i, 3) 
    End With 

    '// output table (in Sheet2) 
    With Worksheets("Sheet2") 
     .Cells(chkROW, chkCOL) = chkVAL 
    End With 
Next i 

'// Add headers to Output table 
For i = 1 to 3 
    With Worksheets("Sheet2") 
     .Cells(i + 1, 1) = testArray(i) 
     .Cells(i, i + 1) = testArray(i) 
    End With 
Next i 
End Sub 
0

您也可以在不使用VBA的情況下執行此操作。

假設您的數據表位於A1:C9的範圍內。 假設3乘3格數據中的第一個數字(0)是單元格F3,上面一行中的A,B,C以及左側列中的A,B,C。

在3×3網格該式中輸入小區F3中的公式作爲

=INDEX($C$1:$C$9,SUMPRODUCT(--($A$1:$A$9=$E3),--($B$1:$B$9=F$2),ROW($A$1:$A$9))) 

複製到所有9個細胞。

這被推廣到任何大小的數據。