FormulaR1C1 - 1004运行时错误,应用程序或对象定义的错误

时间:2014-09-02 18:22:22

标签: excel vba excel-vba

尝试运行以下行时出现1004错误。我很确定它的结尾是字符串。

'Refresh Pivot Table
Sheets("Position").Activate

Sheets("Position").PivotTables("PositionPivot").RefreshTable

Range("D5").Select
Selection.ShowDetail = True  


LastRow = Range("E6").End(xlDown).Offset(1, 0).Row 'if I use a breakpoint, LastRow contains '9'

Range("H6:H" & CStr(LastRow)).FormulaR1C1 = "=RC[-2]-RC[-1]" 'error thrown here

(范围是一个动态变量,我将其设为静态用于问题目的)

1 个答案:

答案 0 :(得分:0)

对于阅读此内容的任何人都会遇到同样的错误:

显然

R1C1

之后

Forumula 

做了些坏事。

以下代码有效:

            'Dim cellcontent As String 'this appearently yields a double declaration
        For q = LBound(A, 1) To UBound(A, 1)
            For Z = LBound(A, 2) To UBound(A, 2)
                msg1 = msg1 & " q = " & q & " and z = " & Z & " " & B(q, 0) / A(q, Z) & vbNewLine 'just writing some message for myself
                'msg1 = msg1 & " q = " & q & " and z = " & Z & " " & B(q, 0) & vbNewLine 'another message
                MsgBox (ThisWorkbook.Worksheets("Solving").Cells(2 + q, 2 * Z + 2).Value) ' message can be ignored
                ThisWorkbook.Worksheets("Solving").Cells(2 + q, 2 * Z + 2).Value = A(q, Z) 'place absorbed amounts /kcal in cells
                ThisWorkbook.Worksheets("Solving").Cells(2 + q, 2 * Z + 3).Activate

                'trying to write basically this to the cell:
                '=B2*C$14
                'that should be the goal for q = 0 and z = 0
                'and for q = 1, z = 0:
                '=B3*C$14
                'and for q = 1, z = 1:
                '=B3*E$14

                cellcontent = "=" & Col_Letter(2 * Z + 2) & 2 + q & "*" & Col_Letter(2 * Z + 3) & "$" & "14" 'generate string
                MsgBox (cellcontent) 'yes yields: =B2*C$14
                'ActiveCell.FormulaR1C1 = "=" & Col_Letter(2 * Z + 2) & 2 + q & "*" & Col_Letter(2 * Z + 3) & "$" & "14" 'Yields error
                'ActiveCell.FormulaR1C1 = cellcontent 'yields error
                ActiveCell.FormulaR1C1 = _
                "=" & Col_Letter(2 * Z + 2) & 2 + q & "*" & "(VLOOKUP(mealgenerator!RC,'01001600'!C3:C5,2,FALSE))" 'yields no error

'                     ActiveCell.FormulaR1C1 = _
'                    "=" & Col_Letter(2 * Z + 2) & 2 + q & "*" & "$" & "(VLOOKUP(mealgenerator!RC,'01001600'!C3:C5,2,FALSE))" 'YIELDS ERROR

                ActiveCell.Formula = _
                cellcontent 'WORKS!


            Next Z
        Next q