VBA 运行时错误“1004”:应用程序定义或对象定义的错误“设置单元格内容

VBA Runtime Error '1004': Application-defined or Object-defined error" setting cell contents

我在设置单元格内容时遇到错误 1004,特别是在行中设置单元格内容时

 ActiveCell.FormulaR1C1 = "=""Total increase in GBP ""&MENU!R11C10"""

在多个工作表中使用了相同的代码,有些工作正常,而其他工作表遇到此错误。基本上我试图添加带有以“21BG”开头的参考代码的行总数,然后打印描述

“英镑总增幅 £TOTAL”

“英镑总减少额 £TOTAL” 取决于总数。

宏如下:

Sub AdjustGBP()
'
' Macro4 Macro


    Range("B65536").Select
    Selection.End(xlUp).Select
    ActiveCell.Offset(1, 0).Select
    ActiveCell.FormulaR1C1 = "21BG"

    ActiveCell.Offset(-1, 1).Select
    Selection.Copy
    ActiveCell.Offset(1, 0).Select
    Selection.PasteSpecial Paste:=xlPasteValues
  
    ActiveCell.Offset(0, 1).Select
    ActiveCell.FormulaR1C1 = "11041202"
    
    ActiveCell.Offset(0, 1).Select
    ActiveCell.FormulaR1C1 = "Current deposits GBP"

    ActiveCell.Offset(0, 1).Select
    ActiveCell.FormulaR1C1 = "当座預金 GBP"
   
    ActiveCell.Offset(0, 5).Select
    Selection.Copy
    If ActiveCell.Value > 0 Then

    ActiveCell.Offset(0, -2).Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlSubtract
    
    ActiveCell.Offset(0, -2).Select
'    ActiveCell.FormulaR1C1 = "Total increase in GBP XXX 2007"
    ActiveCell.FormulaR1C1 = "=""Total increase in GBP ""&MENU!R11C10"""
    
    Else
    
    ActiveCell.Offset(0, -1).Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlSubtract

    ActiveCell.Offset(0, -3).Select
'    ActiveCell.FormulaR1C1 = "=""Total Decrease in " & "MENU!R11C10""
    ActiveCell.FormulaR1C1 = "=""Total Decrease in GBP ""&MENU!R11C10&"" 2021"""

End If

    ActiveCell.Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False

    Range(ActiveCell.Offset(-3, -5), ActiveCell.Offset(0, 4)).Select
    With Selection.Borders(xlEdgeBottom)
        .Weight = xlMedium
    End With
    With Selection.Borders(xlInsideHorizontal)
        .Weight = xlThin
    End With
    
End Sub

不要忘记使用“&”运算符连接字符串。

ActiveCell.FormulaR1C1 = "=" & Chr(34) & "英镑总涨幅" & Chr(34) & "&MENU!R11C10"

这一行

ActiveCell.FormulaR1C1 = "=""Total increase in GBP ""&MENU!R11C10"""

公式看起来像

="Total increase in GBP "&MENU!R11C10"

所以它最后有一个 " 不应该在那里!

该行应该看起来像

ActiveCell.FormulaR1C1 = "=""Total increase in GBP ""&MENU!R11C10"

得到这个公式

="Total increase in GBP "&MENU!R11C10