【问题标题】:Excel VBA - 1004 run-time error, Application or object-defined errorExcel VBA - 1004 运行时错误、应用程序或对象定义错误
【发布时间】:2011-07-11 20:49:50
【问题描述】:

我正在尝试浏览工作表中的一系列单元格并在每个单元格中编写一个公式。 但我不断收到错误:

Run-time error '1004'

Application-defined or object-defined error

代码现在看起来像这样:

Sub updateFormulasForNamedRange()
    'Application.Calculation = xlCalculationManual
    'Application.ScreenUpdating = False

    Dim row, col, fieldCount As Integer
    colCount = 13
    RowCount = 60

    For col = 1 To colCount
        For row = 1 To RowCount
            Dim strColCharacter

            If col > 26 Then
                strColCharacter = Chr(Int((row - 1) / 26) + 64) & Chr(((row - 1) Mod 26) + 65)
            Else
                strColCharacter = Chr(row + 64)
            End If

            Worksheets("Rawdata1").Cells(row, col).FormulaR1C1 = "=IF(Numbers1!$E$" & col & "<>0;Numbers1!" & "$" & strColCharacter & "$" & row & ";"""")"

        Next row
    Next col

    'Application.Calculation = xlCalculationAutomatic
    'Application.ScreenUpdating = True
End Sub

它在您将公式分配给单元格的行处失败。我试图用“test”替换字符串并且它有效。但这是不被接受的字符串。即使它与当前在该确切单元格的公式栏中的字符串完全相同。 我觉得这个字符串很好?

"=IF(Numbers1!$E$1<>0;Numbers1!$A$1;"")"

我不太清楚所有公式属性的区别,但我尝试了它们的变体并且都抛出了相同的错误。那么是什么导致了这个错误呢?

【问题讨论】:

    标签: excel syntax excel-formula vba


    【解决方案1】:

    您的问题在于 .FormulaR1C1。这告诉公式需要一个行号、列号样式的公式引用,然后你给它一个地址(列、行)样式的公式。

    将 .FormulaR1C1 更改为 .Formula

    【讨论】:

    • 不过,同样的事情发生了。我也尝试过 FormulaLocal 。好像没有。
    • 我发现了错误,它在字符串中,因为我认为它必须有。更新答案
    • 如果是@Kenny 的回答,最好将他的回答设置为“已接受”......然后您的主题将被标记为已回答。
    【解决方案2】:

    字符串中有错误:

    Worksheets("Rawdata1").Cells(row, col).FormulaR1C1 = "=IF(Numbers1!$E$" & col & "<>0;Numbers1!" & "$" & strColCharacter & "$" & row & ";"""")"
    

    应该是:

    Worksheets("Rawdata1").Cells(row, col).FormulaR1C1 = "=IF(Numbers1!$E$" & row & "<>0;Numbers1!" & "$" & strColCharacter & "$" & row & ";"""")"
    

    以行为目标,而不是列。

    【讨论】:

      猜你喜欢
      • 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
      相关资源
      最近更新 更多