简体   繁体   English

在不使用Excel VBA中的内置功能的情况下执行Sumproduct

[英]Performing a Sumproduct without using the built-in function in Excel VBA

I've simplified my problem for the purposes of asking this question. 为了提出这个问题,我已经简化了我的问题。 Imagine I have the following data in Excel: 想象一下我在Excel中具有以下数据:

Cashflows | 现金流量 DiscountFactor DiscountFactor

11,851 | 11,851 | 0.9901 0.9901

96,401 | 96,401 | 0.9679 0.9679

80,412 | 80,412 | 0.9494 0.9494

I want to perform a SUMPRODUCT on these two columns in VBA but without using the built in SUMPRODUCT Function. 我想在VBA中的这两列上执行SUMPRODUCT,但不使用内置的SUMPRODUCT函数。 This is because my project is going to get more complicated than that - but I just need to figure this out to begin with. 这是因为我的项目将变得比这更复杂-但我只需要弄清楚这一点即可。 My code so far: 到目前为止,我的代码:

Sub DiscountedCashflows()

Dim CashFlows(1 To 3) As Variant
Dim DiscountFactors(1 To 3) As Variant
Dim CashFlowsElms 'Elements in the cashflow array
Dim DiscountFactorElms 'Elements in the DiscountFactors array

Sheet1.Select

'Populating Cashflows array
Dim Counter1 As Long
For Counter1 = LBound(CashFlows) To UBound(CashFlows)
    CashFlows(Counter1) = Range("A1").Offset(Counter1, 0).Value
Next Counter1

'Populating DiscountFactors array
Dim Counter2 As Long
For Counter2 = LBound(DiscountFactors) To UBound(DiscountFactors)
    DiscountFactors(Counter2) = Range("B1").Offset(Counter2, 0).Value
Next Counter2

'Loop through the elements in the first array
For Each CashFlowsElms In CashFlows
    'Loop through the elements in the second array
    For Each DiscountFactorElms In DiscountFactors
        x = x + 1
        'Multiply the two array elements together
        Cells(x, 1) = CashFlowElms * DiscountFactorElms
    Next DiscountFactorElms
Next CashFlowsElms

MsgBox "Answer is..."

Erase CashFlows
Erase DiscountFactors

End Sub

How do I get the code to output the correct answer? 如何获取代码以输出正确答案?

To give some context I'll be expanding this to work for dynamic arrays and I'll eventually turn it into a user defined function. 为了提供一些背景信息,我将对其进行扩展以适用于动态数组,并最终将其转换为用户定义的函数。

Any help is appreciated. 任何帮助表示赞赏。

The main problem is you only want to loop once and refer to the same row. 主要问题是您只想循环一次并引用同一行。 To do that you would use a simple for loop and use the counter as the index of both arrays. 为此,您将使用简单的for循环并将计数器用作两个数组的索引。

There is no reason to load the arrays with a loop, as you can assign the values directly. 您无需循环加载数组,因为您可以直接分配值。

Sub DiscountedCashflows()

Dim CashFlows() As Variant
Dim DiscountFactors() As Variant
Dim i As Long
Dim temp As Double
With Worksheets("Sheet16")
    'Populating Cashflows array
    CashFlows = .Range("A2:A4").Value
    'Populating DiscountFactors array
    DiscountFactors = .Range("B2:B4").Value

    'Loop through the elements in the first array
    For i = LBound(CashFlows, 1) To UBound(CashFlows, 1)
        temp = temp + (CashFlows(i, 1) * DiscountFactors(i, 1))
    Next i
End With

MsgBox "Answer is..." & temp


End Sub
Function MySumProduct() As Double
    Dim CashFlows As Variant
    Dim DiscountFactors As Variant

    With Sheet1
        CashFlows = Application.Transpose(.Range("A1", .Range("A1").End(xlDown)).Value)
        DiscountFactors = Application.Transpose(.Range("B1", .Range("B1").End(xlDown)).Value)
    End With

    For i = LBound(CashFlows) to UBound(CashFlows)
        MySumProduct= MySumProduct + CashFlow(i) * DiscountFactor(i)
    Next i
End Function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM