我有一个创建数组数组的函数,以及一个应该得到结果数组并将其写入电子表格的函数。我没有找到允许我将数组数组传递给第二个函数的语法......你能帮忙吗?
例如,如果arr1和arr2每个包含24个项目(它们总是包含相同数量的项目),我希望生成的数组是一个大小为24 x 2的2维数组,我希望生成的2 x 24表格为写入电子表格,例如范围A1:B24
这是我的代码:
创建数组数组的函数:
Function GetCellDetails(dict1 As Dictionary, dict2 As Dictionary) As Variant
Dim arr1, arr2
arr1 = dict1.Items
arr2 = dict2.Items
GetCellDetails = Array(arr1, arr2)
End Function
将其写入电子表格的功能:
Sub WriteCellDataToMemory(arr As Variant, day As Integer, cellId As Integer, nCells As Integer)
row = CellIdToMemRow(cellId, nCells)
col = DayToMemCol(day)
arrSize = UBound(arr, 2) 'compiler error
Range(Cells(row, col), Cells(row + arrSize , col + 2)) = Application.Transpose(arr)
End Sub
调用函数的代码:
Dim CellDetails
CellDetails = GetCellDetails(dict1, dict2)
WriteCellDataToMemory CellDetails, day, cellId, nCells
我收到编译错误:
arrSize = UBound(arr, 2)
,因为编译器不知道arr是2 dim数组...
谢谢,
李
答案 0 :(得分:3)
创建的CellsDetails
实际上是一维数组。 Ubound(arr)
完成了这项工作。
Sub Main()
Cells.ClearContents
Dim d1 As New Dictionary
Dim d2 As New Dictionary
d1.Add 1, "1"
d1.Add 2, "2"
d2.Add 3, "3"
d2.Add 4, "4"
Dim CellDetails
CellDetails = GetCellDetails(d1, d2)
WriteCellDataToMemory CellDetails
End Sub
Function GetCellDetails(dict1 As Dictionary, dict2 As Dictionary) As Variant
Dim arr1, arr2
arr1 = dict1.Items
arr2 = dict2.Items
GetCellDetails = Array(arr1, arr2)
End Function
Sub WriteCellDataToMemory(arr As Variant)
Dim arrSize As Long
arrSize = UBound(arr)
Range(Cells(1, 1), Cells(1 + arrSize, arrSize+1)) = Application.Transpose(arr)
End Sub
也许插图可以帮助您理解
所以你有一个多维obj1
到objX
你将它们粘贴在一个存储为对象的一维数组中