将二维数组发布到Excel工作表

该function作为“资产类别分配”引擎(在Param范围内具有约束条件),并在数组的每一行上模拟投资组合模型。 我尝试用四种方法将数组发布到工作表上,每个方法都失败。

对于资产A,B,C,D中的每个资产权重,这些参数在M3:O6中被configuration为{最小5,最大100,步骤5}。

该函数无法将2D数组粘贴到Excel中。 有970个排列,所以排列是970×5。 这是一个尺寸问题? 代码工作正常PrintArray AllocArray, ActiveWorkbook.Worksheets("Test").[A1]

Sub PrintArray(Data As Variant, Cl As Range) Cl.Resize(UBound(Data, 1), UBound(Data, 2)) = Data End Sub

 Function ConfigureArrayFolly() Dim Param() As Variant Param = Range("M3:O6") Dim AMin, AMax, AStep, BMin, BMax, BStep, CMin, CMax, CStep, DMin, DMax, DStep As Double AMin = Param(1, 1): AMax = Param(1, 2): AStep = Param(1, 3) BMin = Param(2, 1): BMax = Param(2, 2): BStep = Param(2, 3) CMin = Param(3, 1): CMax = Param(3, 2): CStep = Param(3, 3) DMin = Param(4, 1): DMax = Param(4, 2): DStep = Param(4, 3) Dim nSim As Double: nSim = (1 + (AMax - AMin) / AStep) * (1 + (BMax - BMin) / BStep) * (1 + (CMax - CMin) / CStep) * (1 + (DMax - DMin) / DStep) Dim nAsset As Double: nAsset = 4 ' Count {A, B, ... , F} 'Debug.Print nSim Dim AllocArray() As Variant ReDim AllocArray(1 To 970, 0 To nAsset) Dim Sim As Integer: Sim = 1 Dim A As Double Dim B As Integer Dim C As Integer Dim D As Integer For A = AMin To AMax Step AStep For B = BMin To BMax Step BStep 'If (A + B) > 100 Then GoTo endB For C = CMin To CMax Step CStep 'If (A + B + C) > 100 Then GoTo endC For D = DMin To DMax Step DStep ' nAsset is the count of set {a1, a2 ... an} ' AllocArray(1, 2, 3) = (Sim, a1, a2) 'Constraints If (A + B + C + D) <> 100 Then GoTo endD Debug.Print Sim; A; B; C; D AllocArray(Sim, 0) = Sim AllocArray(Sim, 1) = A AllocArray(Sim, 2) = B AllocArray(Sim, 3) = C AllocArray(Sim, 4) = D Sim = Sim + 1 ' Debug.Print "Sim "; Sim; AllocArray(1, 1) endD: Next D endC: Next C endB: Next B Next A ' Print to sheet - Method One (fails) Dim NumRows As Long: Dim NumCols As Long NumRows = UBound(AllocArray, 1) - LBound(AllocArray, 1) + 1 NumCols = UBound(AllocArray, 2) - LBound(AllocArray, 2) + 1 Set Destination = Range("D20").Resize(NumRows, NumCols).Value = AllocArray ' Print to sheet - Method Two (fails) 'Sheets("Test").Range("D20").Value = AllocArray(1, 1) 'Print to sheet - Method Three (fails) PrintArray AllocArray, ActiveWorkbook.Worksheets("Test").[D20] 'Print to sheet - Method Four (fails) Range("D20:H989").Value = AllocArray Sheets("Test").Range("D20").Resize(Sim, NumCols).Value = AllocArray 'Range(D20:G6002) = AllocArray ConfigureArrayFolly = nSim End Function 

您的数组对于每个维度具有不同的下限。

您需要通过将UBound(Data,2)加1来调整:

 Sub PrintArray(Data As Variant, Cl As Range) Cl.Resize(UBound(Data, 1), UBound(Data, 2) + 1) = Data End Sub 

几点提示。

  • 你使用Range("D20").Resize(NumRows, NumCols).Value = AllocArray是正确的Range("D20").Resize(NumRows, NumCols).Value = AllocArray语法。
  • AllocArray需要使用ReDim AllocArray(1 to NumRows, 1 to NumCols)
  • 它被正确定义为Dim AllocArray() as Variant
  • 内容需要是DoubleString 。 你是混合整数和双打,我认为Excel很难与此(我可能是错的)。 设置ABCDDouble
  • Dim nAsset As Integer: nAsset = 5 ' Count {A, B, ... , F}这是一个整数,它为什么被定义为Double
  • 使用32位整数进行计数。 Dim Sim As Long: Sim = 1 。 最大。 Integer值是32767,所以你可能溢出。 这可能也适用于您的代码中的其他Integertypes。
  • 你已经知道数组有多大,你不需要UBound()LBound()调用。