我當前讀取的2維範圍到Excel VBA陣列像這樣:計數數
Set Ws = Sheet1
Ws.Activate
LastRow = Ws.Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row
LastCol = Ws.Cells.Find("*", searchorder:=xlByColumns, searchdirection:=xlPrevious).Column
ReDim elements(0 To LastRow - 2, 0 To LastCol - 2)
elements = Ws.Range(Cells(2, 1), Cells(LastRow, LastCol))
範圍是25行乘11列。但是,並非該範圍內的所有單元格都具有值,因此數組中的某些值爲「空」。
- 山口A具有25項
- 列B具有16
- COL K具有12 ...
我通過這個陣列需要循環和創建的第二陣列,這將是一個「笛卡爾產品」的值從第一個。爲了確定我需要循環多少次,我需要弄清楚每個數組列中有多少項(「維度」?)。
這裏是我的循環的嘗試:
Row = 0
For i = 1 To 25 'numElements in column 1
For j = 1 To 3 'numElements in column 6
For k = 1 To 5 'numElements in column 7
For l = 1 To 14 'numElements in column 8
For m = 1 To 6 'numElements in column 10
For n = 1 To 12 'numElements in column 11
cartesian(Row, 0) = elements(i, 0)
cartesian(Row, 1) = elements(i, 1)
cartesian(Row, 2) = elements(i, 2)
cartesian(Row, 3) = elements(i, 3)
cartesian(Row, 4) = elements(i, 4)
cartesian(Row, 5) = elements(j, 5)
cartesian(Row, 6) = elements(k, 6)
cartesian(Row, 7) = elements(l, 7)
cartesian(Row, 8) = elements(l, 8)
cartesian(Row, 9) = elements(m, 9)
cartesian(Row, 10) = elements(n, 10)
Row = Row + 1
Next n
Next m
Next l
Next k
Next j
Next i
任何幫助表示讚賞:)
編輯1:
這是我讀入陣列1的範圍內:
Austria sem jan
Belgium gdn feb
France mar
US apr
may
jun
我需要能夠統計多少「項目「在第1欄,第2欄和第3欄中以便將它們相乘。這樣我會知道我需要多大的ReDim第二陣列。
這是我所需要的陣列2和最後寫回另一片:
Austria sem jan
Austria sem feb
Austria sem mar
Austria sem apr
Austria sem may
Austria sem jun
Austria gdn jan
Austria gdn feb
Austria gdn mar
Austria gdn apr
Austria gdn may
Austria gdn jun
Belgium sem jan
Belgium sem feb
Belgium sem mar
Belgium sem apr
Belgium sem may
Belgium sem jun
Belgium gdn jan
Belgium gdn feb
Belgium gdn mar
Belgium gdn apr
Belgium gdn may
Belgium gdn jun
等
我認爲這可能會超過它。你的目標是什麼?在一個範圍內找到空的細胞?那麼,對於第1,6,7,8,10和11列,你想要找到空單元格? – BruceWayne
或者Alex只想將第2列至第11列的「非空」值與第1列的值相結合? – Kellsens
我也不明白它......去*笛卡爾積*但沒有計算......它只是第一個範圍的副本(只是值被混洗)。那麼開始時有什麼不同?每一行?但有n套也提供了一個n維輸出...我很困惑:/ –