Excel VBA宏调试 - 应用程序/对象定义的错误(错误1004)

时间:2015-02-13 21:36:44

标签: excel vba excel-vba

我正在为我的同事调试一个宏,我不是代码的原始编写者。话虽如此,问题是在excel更新时创建的,导致宏变得有缺陷 - 应用程序定义或对象定义错误(ERROR'1004')

以下是代码的特定部分:

下一个n_counter

'calculate aggregated results
For counter_res = 1 To 7
    'insert variance of null distribution
        Worksheets("results").Cells(22 + counter_res, 2).Value = _
        Worksheets("L_S_2008").Cells(2 + counter_res, no_anchors).Value
    'mean values of agreement index
        Worksheets("results").Cells(22 + counter_res, 3).Value = _
        WorksheetFunction.Average(Range(Cells(3, 18 + counter_res), _
        Cells(5000, 18 + counter_res)))
    'SD of agreement index
        Worksheets("results").Cells(22 + counter_res, 4).Value = _
        (WorksheetFunction.Var(Range(Cells(3, 18 + counter_res), _
        Cells(5000, 18 + counter_res)))) ^ 0.5
    'P25 of agreement index
        Worksheets("results").Cells(22 + counter_res, 6).Value = _
        WorksheetFunction.Percentile(Range(Cells(3, 18 + counter_res), _
        Cells(5000, 18 + counter_res)), 0.25)
    'Median (P50) of agreement index
        Worksheets("results").Cells(22 + counter_res, 7).Value = _
        WorksheetFunction.Percentile(Range(Cells(3, 18 + counter_res), _
        Cells(5000, 18 + counter_res)), 0.5)
    'P25 of agreement index
        Worksheets("results").Cells(22 + counter_res, 8).Value = _
        WorksheetFunction.Percentile(Range(Cells(3, 18 + counter_res), _
        Cells(5000, 18 + counter_res)), 0.75)

调试返回了第一行代码

[Worksheets("results").Cells(22 + counter_res, 2).Value = Worksheets("L_S_2008").Cells(2 + counter_res, no_anchors).Value]

作为错误的来源。

如果有人有一些反馈或建议,我会非常荣幸。感谢您提前看一下这个问题,非常感谢。

2 个答案:

答案 0 :(得分:1)

工作表的名称不正确,或者变量no_anchors没有列的有效值。调试时,no_anchors有什么价值......?如果为0,那就是问题所在。不能具有列值<1的单元格。 1。

答案 1 :(得分:0)

你可以通过删除所有重复来大量收紧这些代码 - 这可能有助于调试。

当前问题的可能原因是smackenzie所指出的no_anchors中的错误值,或者用于计算的输入表不是您期望的输入表(因为它未明确指定)< / p>

Dim rngCalc As Range, wsf As WorksheetFunction
Set wsf = Application.WorksheetFunction


'calculate aggregated results
For counter_res = 1 To 7

    'What sheet is this intended to reference?
    'By default it will be the ActiveSheet unless specified
    Set rngCalc = Range(Cells(3, 18 + counter_res), _
                        Cells(5000, 18 + counter_res))

    With Worksheets("results").Rows(22 + counter_res)

        .Cells(2).Value = Worksheets("L_S_2008").Cells(2 + counter_res, _
                            no_anchors).Value           'variance

        .Cells(3).Value = wsf.Average(rngCalc)          'Mean
        .Cells(4).Value = wsf.Var(rngCalc) ^ 0.5        'SD
        .Cells(6).Value = wsf.Percentile(rngCalc, 0.25) 'P25
        .Cells(7).Value = wsf.Percentile(rngCalc, 0.5)  'Median(P50)
        .Cells(8).Value = wsf.Percentile(rngCalc, 0.75) 'P75

    End With

Next counter_res