Excel VBAmacrosdebugging – 应用程序/对象定义的错误(错误1004)

我正在为我的一个同事debugging一个macros,我不是代码的原始编写者。 这就是说,这个问题是在excel更新时创build的,导致macros成为缺陷 – 应用程序定义或对象定义的错误(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) 

debugging返回了第一行代码

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

作为错误的来源。

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

或者页面命名不正确,或者variablesno_anchors没有获得有效的列值。 当你debugging的时候,no_anchors有什么价值…? 如果是0,那就是问题所在。 不能有一个列值<1的单元格。

您可以通过删除所有重复来收紧代码 – 这可能有助于debugging。

当前问题的可能原因在于no_anchors指出的no_anchors中是一个不好的值,或者用于计算的input表不是明确的(因为它没有明确指定)

 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