替换公式时出现运行时错误1004 [英] Runtime Error 1004 when replacing formula
问题描述
我正在使用以下代码在VBA中向公式添加一些内容(出于调试目的,我使用变量currentFormula,而不是直接执行此操作):
I am using the following code to add something to a Formula in VBA (for debugging purposes I am using the variable currentFormula, instead of doing it directly):
currentFormula = Range("A" & row).Formula
currentFormula = currentFormula & "+" & CStr(difference)
Range("A" & row).Formula = currentFormula
逐步执行代码时,变量currentFormula在最后一步之前具有正确的值,例如"= A1/A2 + 0.5".但是,该脚本将失败,并出现运行时错误1004.当我将currentFormula手动设置为"= 10 + 10"之类的脚本时,该脚本可以工作.
When going through the code step by step, the variable currentFormula has the correct value before the final step, e.g. "=A1/A2+0.5". However, then the script Fails with runtime error 1004. When I am Setting currentFormula manually to something like "=10+10", the script works.
推荐答案
CStr
根据当前系统区域设置格式化数字. Formula
接受英语公式.
CStr
formats the number according to the current system locale.
Formula
accepts formulas in English.
以不变的方式将数字转换为字符串的函数是 Str
.请注意,它为您可能要删除的正数添加了一个空格:
The function that converts numbers to strings in an invariant way is Str
. Note that it prepends a space to positive numbers which you might want to remove:
currentFormula = currentFormula & "+" & LTrim$(Str$(difference))
这篇关于替换公式时出现运行时错误1004的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!