简体   繁体   English

Excel VBA-运行时错误'1004'

[英]Excel VBA - Runtime error '1004'

I have some VBA code where I want to copy a formula into a range of cells using R1C1 . 我有一些VBA代码,我想在其中使用R1C1将公式复制到一系列单元格中。 Unfortunately I get the following error on the second to last line: 不幸的是,倒数第二行出现以下错误:
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

What am I doing wrong here? 我在这里做错了什么?

您在公式中可能缺少括号

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

I think you are running non-english version of Excel/Office. 我认为您正在运行非英语版本的Excel / Office。 Therefore there could be a problem of numbers separation mark. 因此可能存在数字分隔标记的问题。 It's required to use . (dot) 必须使用. (dot) . (dot) in function defined as string and passed then by formulaR1C1 property to Excel. 函数中的. (dot)定义为字符串,然后由formulaR1C1 property传递给Excel。 It will be then converted into , (comma) which is required when putting function in excel cell. 然后将其转换为, (comma) ,这是将函数放入excel单元格所需的。 Therefore this lines works for me with no error: 因此,这行对我有用,没有错误:

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

One more thing, I removed '' (single quotation marks) for sheet name. 还有一件事,我删除了工作表名称的'' (single quotation marks)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM