私有功能不同导致步进和运行模式

时间:2015-01-16 16:03:10

标签: excel-vba runtime step-into vba excel

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

    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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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技能!

干杯