Excel VBA – 运行时错误“1004”

我有一些VBA代码,我想要使用R1C1将公式复制到单元格范围内。 不幸的是我在倒数第二行得到以下错误:
Runtime error '1004': Application-defined or object-defined error.

 Dim pct As Single pct = 1 With ThisWorkbook.Sheets("Data").UsedRange Dim lastR As Long lastR = .Rows.Count Dim lastC As String lastC = col_letter(.Columns.Count) End With With ThisWorkbook.Sheets("Calculations") .Range("A1:" & lastC & 1).Value = 100 ==> .Range("A2:" & lastC & lastR + 1).FormulaR1C1 = "=R[-1]C*(1+" & pct / 100 & "*'Data'!R[-1]C" End With 

我在这里做错了什么?

我想你正在运行非英文版的Excel / Office。 因此可能会出现数字分隔标记的问题。 这是需要使用的. (dot) . (dot)函数定义为string,然后通过formulaR1C1 property传递给Excel。 然后将它转换成, (comma)将函数放入excel单元格时所需要的, (comma) 。 因此这条线对我来说没有任何错误:

 ....=Replace("=R[-1]C*(1+" & pct / 100 & ")*Data!R[-1]C", ",", ".") 

还有一件事,我删除了表单名称'' (single quotation marks)

你错过了公式中的括号 – 也许

 "=R[-1]C*(1+" & pct / 100 & ")*'Data'!R[-1]C"