My code runs without errors in both step through mode and run mode. However, it is only in step through mode that I get the correct results of the calculations in the code. It is not clear where the wrong results come from in run mode.
Private Function get_totals(sh As Worksheet, lastrowi As Long, rowi As Integer, n As Double, o As Integer, k As Integer, totals_sheet As Worksheet, arearange As Range)
k = 2
lastrowi = Application.WorksheetFunction.CountA(arearange)
For rowi = k To lastrowi
totals_sheet.Cells(rowi, 12).Value = Application.Sum(Range(Cells(rowi, 2), Cells(rowi, 4)))
n = Application.Sum(Range(Cells(rowi, 6), Cells(rowi, 11)))
totals_sheet.Cells(rowi, 13).Value = n / o
Next rowi
End Function
I assume the problem is that it is referencing different sheets/cells when in run mode but as I set my variables outside of the function (code below) I am unsure where the problem arises. Anyone with fresh eyes able to spot the cause of error?
For Each sh In Sheets(Array("pipe_totals", "node_totals")) 'needs expanding once the calcs sheets are in
If sh.Name = "pipe_totals" Then
Set sh1 = Sheets("pipe_diam_calcs")
Set totals_sheet = Sheets("pipe_totals") 'will change for each asset group node/wps/reservoir/address
Set arearange = totals_sheet.Columns("A:A") ' will change for node/wps/reservoir/address
Set dmalist = sh1.Columns("c:c")
o = 6
ElseIf sh.Name = "node_totals" Then
Set sh1 = Sheets("node_z_calcs")
Set totals_sheet = Sheets("node_totals") 'will change for each asset group node/wps/reservoir/address
Set arearange = totals_sheet.Columns("A:A") ' will change for node/wps/reservoir/address
Set dmalist = sh1.Columns("c:c")
o = 2
End If
Call getdma_list(dmalist, arearange)
Call loop_weight_tot(sh, totals_sheet, arearange, sh2, rowi, row, rowW, dma_string, k, col, colNum, colNum_new)
Call get_totals(sh, lastrowi, rowi, n, o, k, totals_sheet, arearange) 'need to be defined outside of function???
Next sh
Application.ScreenUpdating = True
End Sub