在excel中插入multidimensional array
我想在Excel中插入数据。 作为一个数据源我有一个multidimensional array:说,我有这个
dim myArray(5, 3) for row = 0 to 4 for col = 0 to 2 myArray(row, col) = row * col next next
如何插入这些数组在Excel单元格索引[0,0]
结果
ABC 1 0 0 0 2 0 1 2 3 0 2 4 4 0 3 6 5 0 4 8
Excel数组(包括单元索引)是基于1的,而不是像VBScript数组那样基于0的,即没有单元索引[0, 0]
。 但是,由于偏移是不变的,因此可以通过简单地将1添加到VBScript数组的索引来处理:
for row = 0 to UBound(myArray, 1) for col = 0 to UBound(myArray, 2) xl.Workbooks(1).Sheets(1).Cells(row+1, col+1) = myArray(row, col) next next
或者对于懒惰:
Option Explicit Dim a(4, 2) ' Ubounds Dim r, c For r = 0 To UBound(a, 1) For c = 0 To UBound(a, 2) a(r, c) = r * c Next Next Dim oXls : Set oXls = CreateObject("Excel.Application") Dim oWb : Set oWb = oXls.Workbooks.Add oXls.Visible = True oWb.Sheets(1).Range("A1:C5") = a oXls.Quit
证据:
遇到问题:
据此(你将不得不自己search更新的信息) –
Range("a1:a10").Value = Application.WorksheetFunction.Transpose(myarray)
除了我的其他所有版本的Excel,添加更多的噪音(.Value,.Transpose)可能是必要的。
Sub ertdfgcvb() Dim myArray(5, 3) For Row = 0 To 4 For col = 0 To 2 myArray(Row, col) = Row * col Next Next Dim rng As Range Set rng = Range("A1") For i = LBound(myArray, 1) To UBound(myArray, 1) For j = LBound(myArray, 2) To UBound(myArray, 2) rng.Offset(i, j) = myArray(i, j) Next Next End Sub