我希望能夠將矩形網格數據重新排列到列中。我寫了一些代碼並運行一些MsgBox來測試一些事情,它看起來工作正常,除了最後一行。也就是說,我已經測試過,以確保outputArray實際上以其中的數據和其他各種事物結束。但是,當我嘗試將outputArray中的數據寫入工作表時,它只是輸入所有空格(我有一個以前的版本不會自動刪除原始數據,所以我只能看到第一列被空白)。我認爲這是我看不到的一個簡單的錯誤。你能幫我弄清楚有什麼不對嗎? (Excel 2010)VBA Excel - 將數據矩形網格放入列
Sub PutDataIntoColumn()
' This sub assumes data comes in a rectangular grid
' And that data is read across row 1, then across row 2, and so on
' The data is reordered into a column of data and is output starting at the top left cell of the original rectangular grid
' The original grid of data is erased so only the column remains
Dim inputRange As Range
Dim inputArray() As Variant, outputArray() As Variant
Set inputRange = Application.InputBox(Prompt:="Select Data", Title:="Data Into Column", Type:=8)
inputArray = inputRange.Value
' Erase the data
inputRange.Value = ""
numRows = UBound(inputArray, 1)
numCols = UBound(inputArray, 2)
totalCells = numRows * numCols
ReDim outputArray(totalCells, 1)
firstCellRow = inputRange.Row
firstCellCol = inputRange.Column
For i = 1 To numRows
For j = 1 To numCols
outputArray((i - 1) * numCols + j, 1) = inputArray(i, j)
Next j
Next i
ActiveSheet.Cells(firstCellRow, firstCellCol).Resize(totalCells).Value = outputArray
End Sub
您可能需要轉置數組寫入表單之前。 '... = Application.Transpose(outputArray)' –
@Tim,不是這樣,For循環迭代範圍就好了 –