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

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 に答える