【问题标题】:Excel VBA Macro Debugging - Application/Object-defined error (error 1004)Excel VBA 宏调试 - 应用程序/对象定义的错误(错误 1004)
【发布时间】:2015-02-13 21:36:44
【问题描述】:

我正在为我的一位同事调试宏,而我并不是代码的原始作者。话虽这么说,问题是在更新 excel 时产生的,导致宏有缺陷 - 应用程序定义或对象定义错误(错误'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]

作为错误的来源。

如果有人有任何反馈或建议,我将不胜荣幸。提前感谢您查看问题,非常感谢。

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    要么工作表名称不正确,要么变量 no_anchors 没有获得有效的列值。调试的时候,no_anchors 有什么值...?如果为 0,那就是问题所在。不能有列值

    【讨论】:

      【解决方案2】:

      您可以通过删除所有重复来大大加强该代码 - 这可能有助于调试。

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

      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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多