Estou tentando configurar um cálculo que some todos os valores entre "M" e "X" em cada linha. No entanto, quando eu executar o código, ele só me dá a soma para a última linha na planilha, quando ele deve retornar a soma para a linha 5 na planilha.
O bloco específico em que estou tendo problemas para escrever corretamente é:
For i = 5 To LastRow
If Not IsEmpty(.Range(.Cells(i, 13), .Cells(i, 24))) Then
Orig2016Total = .Range("M" & i).Value + .Range("N" & i).Value + .Range("O" & i).Value + .Range("P" & i).Value _
+ .Range("Q" & i).Value + .Range("R" & i).Value + .Range("S" & i).Value + .Range("T" & i).Value _
+ .Range("U" & i).Value + .Range("V" & i).Value + .Range("W" & i).Value + .Range("X" & i).Value
End If
Next I
A linha .Range("Z" & i).Value = Orig2016Total
, que está em negrito no bloco de código abaixo, deve retornar 780.000 para a soma da 5ª linha, mas retorna a soma da última linha de 1.144.669.
Alguém poderia me ajudar a descobrir por que estou recebendo a soma da última linha quando deveria estar exibindo a soma da quinta linha? Obrigado !!
O código inteiro é:
Function ReduceCost_Percentage()
With Worksheets("Analysis Worksheet")
Dim i As Long
Dim LastRow As Long
LastRow = Range("X" & Rows.Count).End(xlUp).Row
Dim TodayDate As Date
TodayDate = Format(Date, "DD/MM/YYYY")
Dim Orig2016Total As Long
Dim MonthsWithValues As Long
For i = 5 To LastRow
If Not IsEmpty(.Range(.Cells(i, 13), .Cells(i, 24))) Then
Orig2016Total = .Range("M" & i).Value + .Range("N" & i).Value + .Range("O" & i).Value + .Range("P" & i).Value _
+ .Range("Q" & i).Value + .Range("R" & i).Value + .Range("S" & i).Value + .Range("T" & i).Value _
+ .Range("U" & i).Value + .Range("V" & i).Value + .Range("W" & i).Value + .Range("X" & i).Value
End If
Next i
For i = 5 To LastRow
If .Range("D" & i).Value > 0 And IsEmpty(.Range("B" & i).Value) _
And IsEmpty(.Range("C" & i).Value) And Not IsEmpty(.Range("M" & i).Value) _
And Not IsEmpty(Worksheets("Fixed Cost Test Data").Range("B" & i).Value) _
And ((TodayDate >= Worksheets("Fixed Cost Test Data").Range("C" & i).Value And Worksheets("Fixed Cost Test Data").Range("C" & i).Value <= #12/31/2015#) Or Worksheets("Fixed Cost Test Data").Range("C" & i).Value <= #12/31/2015#) Then
.Range("M" & i).Value = ((.Range("M" & i).Value - Worksheets("Fixed Cost Test Data").Range("B" & i).Value) - ((.Range("M" & i).Value - Worksheets("Fixed Cost Test Data").Range("B" & i).Value) * (.Range("D" & i).Value * 0.01))) + Worksheets("Fixed Cost Test Data").Range("B" & i).Value
ElseIf .Range("D" & i).Value > 0 And IsEmpty(.Range("B" & i).Value) _
And IsEmpty(.Range("C" & i).Value) And Not IsEmpty(.Range("M" & i).Value) _
And (IsEmpty(Worksheets("Fixed Cost Test Data").Range("B" & i).Value) Or Worksheets("Fixed Cost Test Data").Range("C" & i).Value > TodayDate Or Worksheets("Fixed Cost Test Data").Range("C" & i).Value > #12/31/2015#) Then
.Range("M" & i).Value = .Range("M" & i).Value - (.Range("M" & i).Value * (.Range("D" & i).Value * 0.01))
End If
Next I
'The code continues the same for columns "N" through "X" then it picks up again as:
For i = 5 To LastRow
.Range("Y" & i).Formula = "=SUM(" & .Range(Cells(i, 13), Cells(i, 24)).Address(False, False) & ")"
MonthsWithValues = Application.WorksheetFunction.CountIfs(Worksheets("Analysis Worksheet").Range(.Cells(i, 13), .Cells(i, 24)), "<>0", Worksheets("Analysis Worksheet").Range(.Cells(i, 13), .Cells(i, 24)), "<>""")
If .Range("D" & i).Value > 0 And IsEmpty(.Range("B" & i).Value) And IsEmpty(.Range("C" & i).Value) Then
If .Range("X" & i).Value > 0 And Not IsEmpty(Worksheets("Fixed Cost Test Data").Range("B" & i).Value) _
And Worksheets("Fixed Cost Test Data").Range("C" & i).Value <= #11/30/2016# Then
***.Range("Z" & i).Value = Orig2016Total***
ElseIf .Range("X" & i).Value > 0 And Not IsEmpty(Worksheets("Fixed Cost Test Data").Range("B" & i).Value) _
And Worksheets("Fixed Cost Test Data").Range("C" & i).Value > #11/30/2016# Then
.Range("Z" & i).Value = (Orig2016Total - (Worksheets("Fixed Cost Test Data").Range("B" & i).Value * (12 - Left(Worksheets("Fixed Cost Test Data").Range("C" & i).Value, 2)))) / MonthsWithValues
ElseIf .Range("X" & i).Value > 0 And IsEmpty(Worksheets("Fixed Cost Test Data").Range("B" & i).Value) Then
.Range("Z" & i).Value = Orig2016Total / MonthsWithValues
ElseIf .Range("X" & i).Value = Worksheets("Fixed Cost Test Data").Range("B" & i).Value And Not IsEmpty(Worksheets("Fixed Cost Test Data").Range("B" & i).Value) _
And Not IsEmpty(Worksheets("Fixed Cost Test Data").Range("C" & i).Value) Then
.Range("Z" & i).Value = ((Orig2016Total - (Worksheets("Fixed Cost Test Data").Range("B" & i).Value * (12 - Left(Worksheets("Fixed Cost Test Data").Range("C" & i).Value, 2)))) / MonthsWithValues) + Worksheets("Fixed Cost Test Data").Range("B" & i).Value
ElseIf (IsEmpty(.Range("X" & i).Value) Or .Range("X" & i).Value = 0) And Not IsEmpty(Worksheets("Fixed Cost Test Data").Range("B" & i).Value) _
And Not IsEmpty(Worksheets("Fixed Cost Test Data").Range("C" & i).Value) Then
.Range("Z" & i).Value = (Orig2016Total - (Worksheets("Fixed Cost Test Data").Range("B" & i).Value * (12 - Left(Worksheets("Fixed Cost Test Data").Range("C" & i).Value, 2)))) / MonthsWithValues
ElseIf (IsEmpty(.Range("X" & i).Value) Or .Range("X" & i).Value = 0) And IsEmpty(Worksheets("Fixed Cost Test Data").Range("B" & i).Value) Then
.Range("Z" & i).Value = Orig2016Total / MonthsWithValues
End If
End If
Next i
End With
End Function