我的代码在单步执行模式和运行模式下都运行没有错误。但是,只有在逐步模式下,我才能在代码中获得正确的计算结果。目前尚不清楚运行模式下错误结果的来源。
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
我认为问题在于它在运行模式下引用了不同的工作表/单元格,但是当我将变量设置在函数之外(下面的代码)时,我不确定问题出在哪里。任何有眼光的人都能够发现错误的原因吗?
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