如何将一个数组与其他数组相乘?

时间:2019-07-10 16:30:30

标签: excel vba

用于将2个数组相乘并创建一个新的一维数组的代码

公共函数APortfolioReturns(AReturns()为double,weights()为double)作为Double()         出错时转到errHandler

    Dim arrData() As Double
    Dim TotRows As Long
    Dim TotCols As Integer
    Dim RowCtr As Long
    Dim Colctr As Integer
    Dim dblSum As Double

    TotRows = UBound(AReturns, 1) - LBound(AReturns, 1) + 1
    TotCols = UBound(Weights) - LBound(Weights) + 1

    ReDim arrData(1 To TotRows)

    For RowCtr = 1 To TotRows

        dblSum = 0

            For Colctr = 1 To TotCols

                dblSum = dblSum + (AReturns(RowCtr, Colctr) * Weights(Colctr))

            Next

        arrData(RowCtr) = dblSum

    Next

    APortfolioReturns = arrData()

    Exit Function

errHandler:
    MsgBox "An error has occurred." & vbCrLf & Err.Description & vbCrLf & CStr(Err.Number)

End Function


 I have this 2 arrays and i want to create a new 1 d array as follows:


    11  20  45
    12  21  48
    13  22  48.5
    15  23  50
    18  27  52


    0.5
    0.3
    0.2

   The new array would be as follows: 
    first row = (11*0.5) + (20*0.3) + (45*0.2)
    Second row = (12*0.5) + (21*0.3) + (48*0.2)
    third row = (13**0.5) + (22*0.3) + (48.5*0.2) and so on..


But i am not getting the intended result.

Please help me on this multiplication. 

代码工作正常。它没有给出任何错误。     我在新数组的所有行中得到的结果均为0。 请帮帮我。

0 个答案:

没有答案