Hello,
Hope you are all good.
I am having a trouble with a diagonal sum vba code. Not sure what wrong I am doing.
1st will explain what I am trying to do.
In the image attached herewith is a table. (A9:F13)
The VBA incorrectly references to $A10 in 2nd row whereas it should reference to $A10.
Formula text table is also mentioned for reference.
Output column is where diagonal sum is expected. Have color coded the output column and table cell to show what cells are summed up to get output result.
Here is my vba code.
Hoping to resolve this as this is an example table. I am trying to do away the need of creating a big table in my model and just do a vba based diagonal sum. My model has huge table.
Hope you can help me with it.
Thanks
Sumant
Hope you are all good.
I am having a trouble with a diagonal sum vba code. Not sure what wrong I am doing.
1st will explain what I am trying to do.
In the image attached herewith is a table. (A9:F13)
- Rows A10, A11, A12 should multiply with their respective curve from the curve library and sum them diagonally.
- The multiplication should occur with the correct row and column values, ensuring the diagonal sum is cumulative.
Key Requirements:
- Row 10: A10 * Curve(ABC, M1)
- Row 11: A11 * Curve(CDE, M1) + A10 * Curve(CDE, M2)
- Row 12: A12 * Curve(CDE, M1) + A11 * Curve(CDE, M2) + A10 * Curve(CDE, M3)
The VBA incorrectly references to $A10 in 2nd row whereas it should reference to $A10.
Formula text table is also mentioned for reference.
Output column is where diagonal sum is expected. Have color coded the output column and table cell to show what cells are summed up to get output result.
Here is my vba code.
VBA Code:
Sub CalculateDiagonalSum()
Dim ws As Worksheet
Dim lastRow As Long
Dim i As Long, j As Long
Dim result As Double
Dim numberValue As Double, curveValue As Double
Dim curveRow As Range
Dim curveColIndex As Integer
Dim curveName As String
Dim rowOffset As Integer
Set ws = ThisWorkbook.Sheets("Sheet1")
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
ws.Range("F10:F" & lastRow).ClearContents
For i = 10 To lastRow
result = 0
curveName = ws.Cells(i, "B").Value
Set curveRow = ws.Range("B4:B6").Find(What:=curveName, LookIn:=xlValues, LookAt:=xlWhole)
If Not curveRow Is Nothing Then
For j = 0 To i - 10
rowOffset = i - j
numberValue = ws.Cells(rowOffset, "A").Value
curveColIndex = 3 + j
If curveColIndex <= 5 Then
curveValue = ws.Cells(curveRow.Row, curveColIndex).Value
result = result + (numberValue * curveValue)
End If
Next j
End If
ws.Cells(i, "F").Value = result
Next i
End Sub
Hoping to resolve this as this is an example table. I am trying to do away the need of creating a big table in my model and just do a vba based diagonal sum. My model has huge table.
Hope you can help me with it.
Thanks
Sumant