簡體   English   中英

Excel VBA-運行時錯誤'1004'

[英]Excel VBA - Runtime error '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

我在這里做錯了什么?

您在公式中可能缺少括號

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

我認為您正在運行非英語版本的Excel / Office。 因此可能存在數字分隔標記的問題。 必須使用. (dot) 函數中的. (dot)定義為字符串,然后由formulaR1C1 property傳遞給Excel。 然后將其轉換為, (comma) ,這是將函數放入excel單元格所需的。 因此,這行對我有用,沒有錯誤:

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

還有一件事,我刪除了工作表名稱的'' (single quotation marks)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM