In the AR sheet is an example output where the red cells are the incorrect values, with the corresponding correct values under it.
The macro is called "Sheetloop". Not really sure what the issue is. I have tried possibly changing the variables from long to Double, to no avail.
I figure it has something to do with the <> argument in the sumif formula.
Sub Sheetloop()
Dim ws As Worksheet
Dim rng As Range
Dim lgst_avg As Long
Dim lgst_max As Long
Dim lgst_min As Long
Dim LR As Long
Dim GA_avg As Long
Dim GA_max As Long
Dim GA_min As Long
Dim other_avg As Double
Dim other_max As Double
Dim other_min As Double
GA_avg = Worksheets("GA_AVERAGE").Range("C6")
GA_max = Worksheets("GA_AVERAGE").Range("D6")
GA_min = Worksheets("GA_AVERAGE").Range("E6")
For Each ws In Worksheets
If ws.Name <> "GA_AVERAGE" And ws.Name <> "DC" Then
LR = ws.Range("F" & Rows.Count).End(xlUp).row
ws.Range("O1") = "AVG"
ws.Range("P1") = "Max"
ws.Range("Q1") = "Min"
ws.Range("O2:O" & LR).Formula = "=F2*M2"
ws.Range("P2:P" & LR).Formula = "=E2*M2"
ws.Range("Q2:Q" & LR).Formula = "=D2*M2"
lgst_avg = Application.WorksheetFunction.SumIf(ws.Range("C2:C" & LR), ws.Range("S12"), ws.Range("O2:O" & LR)) / Application.WorksheetFunction.SumIf(ws.Range("C2:C" & LR), ws.Range("S12"), ws.Range("M2:M" & LR))
lgst_max = Application.WorksheetFunction.SumIf(ws.Range("C2:C" & LR), ws.Range("S12"), ws.Range("P2:P" & LR)) / Application.WorksheetFunction.SumIf(ws.Range("C2:C" & LR), ws.Range("S12"), ws.Range("M2:M" & LR))
lgst_min = Application.WorksheetFunction.SumIf(ws.Range("C2:C" & LR), ws.Range("S12"), ws.Range("Q2:Q" & LR)) / Application.WorksheetFunction.SumIf(ws.Range("C2:C" & LR), ws.Range("S12"), ws.Range("M2:M" & LR))
ws.Range("T12") = lgst_avg
ws.Range("U12") = lgst_max
ws.Range("V12") = lgst_min
ws.Range("w12") = ws.Range("T12") / GA_avg
ws.Range("X12").Formula = ws.Range("U12") / GA_max
ws.Range("Y12").Formula = ws.Range("V12") / GA_min
factor_average = Application.WorksheetFunction.Average(ws.Range("w12"), ws.Range("x12"), ws.Range("y12"))
ws.Range("z12").Value = factor_average
other_avg = Application.WorksheetFunction.SumIf(ws.Range("C2:C" & LR), "<>" & ws.Range("S12"), ws.Range("O2:O" & LR)) / Application.WorksheetFunction.SumIf(ws.Range("C2:C" & LR), "<>" & ws.Range("S12"), ws.Range("M2:M" & LR))
other_max = Application.WorksheetFunction.SumIf(ws.Range("C2:C" & LR), "<>" & ws.Range("S12"), ws.Range("P2:P" & LR)) / Application.WorksheetFunction.SumIf(ws.Range("C2:C" & LR), "<>" & ws.Range("S12"), ws.Range("M2:M" & LR))
other_min = Application.WorksheetFunction.SumIf(ws.Range("C2:C" & LR), "<>" & ws.Range("S12"), ws.Range("Q2:Q" & LR)) / Application.WorksheetFunction.SumIf(ws.Range("C2:C" & LR), "<>" & ws.Range("S12"), ws.Range("M2:M" & LR))
ws.Range("T13") = other_avg
ws.Range("U13") = other_max
ws.Range("V13") = other_min
ws.Range("w13") = ws.Range("T13") / GA_avg
ws.Range("X13").Formula = ws.Range("U13") / GA_max
ws.Range("Y13").Formula = ws.Range("V13") / GA_min
other_factor_average = Application.WorksheetFunction.Average(ws.Range("w13"), ws.Range("x13"), ws.Range("y13"))
ws.Range("z13").Value = other_factor_average
End If
Next
End Sub
Bookmarks