0

我的代码在单步执行模式和运行模式下都运行没有错误。但是,只有在逐步模式下,我才能在代码中获得正确的计算结果。目前尚不清楚运行模式下错误结果的来源。

    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
4

2 回答 2

1

您不需要激活工作表(并且您应该始终尽量避免编写依赖于特定工作表处于活动状态的代码)

例如:

With totals_sheet
    .Cells(2, 15).Value = Application.Sum( _
                         .Range(.Cells(2, 12), .Cells(lastrowi, 12)))
    .Cells(2, 16).Value = Application.Average( _
                         .Range(.Cells(2, 13), .Cells(lastrowi, 13)))
End With
于 2015-01-19T17:29:50.190 回答
0

我想出了一个不优雅的解决方案,因为怀疑问题似乎是对单元格/范围的引用是不合格的,但是当我试图用它们来限定它们时,Application.Sum(totals_sheet.Range(Cells(rowi, 6), totals_sheet.Cells(rowi, 11)))我得到对象类_worksheet 的运行时错误范围失败。

我非常不雅的解决方案是激活totals_sheet

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.Activate 'to prevent active sheet returning unpredictable results

            With ActiveSheet
            totals_sheet.Cells(rowi, 12).Value = Application.Sum(Range(Cells(rowi, 2), Cells(rowi, 4)))
            n = Application.Sum(totals_sheet.Range(Cells(rowi, 6), totals_sheet.Cells(rowi, 11)))

            totals_sheet.Cells(rowi, 13).Value = n / o
            End With


    Next rowi

'get total mains/weight for the model
    totals_sheet.Activate
        With ActiveSheet
            totals_sheet.Cells(2, 15).Value = Application.Sum(Range(Cells(2, 12), Cells(lastrowi, 12)))
            totals_sheet.Cells(2, 16).Value = Application.Average(Range(Cells(2, 13), Cells(lastrowi, 13)))
        End With

End Function

我欢迎任何关于如何改进这一点的反馈/批评,以帮助我发展我目前有点缺乏的 VBA 技能!

干杯

于 2015-01-19T10:06:23.537 回答