使用 VBA 在 Excel 单元格中编写公式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/341258/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Write a formula in an Excel Cell using VBA
提问by Matthieu
I'm trying to use VBA to write a formula into a cell in Excel.
My problem is that when I use a semicolon (;) in my formula, I get an error:
我正在尝试使用 VBA 将公式写入 Excel 中的单元格。我的问题是,当我;在公式中使用分号 ( ) 时,出现错误:
Run-time error 1004
Run-time error 1004
My macro is the following :
我的宏如下:
Sub Jours_ouvres()
Dim Feuille_Document As String
Feuille_Document = "DOCUMENT"
Application.Worksheets(Feuille_Document).Range("F2").Formula = "=SUM(D2;E2)"
End Sub
采纳答案by Matthieu
You can try using FormulaLocal property instead of Formula. Then the semicolon should work.
您可以尝试使用 FormulaLocal 属性而不是 Formula。然后分号应该工作。
回答by KnomDeGuerre
The correct character (comma or colon) depends on the purpose.
正确的字符(逗号或冒号)取决于目的。
Comma (,) will sum only the two cells in question.
逗号 ( ,) 将仅对有问题的两个单元格求和。
Colon (:) will sum all the cells within the range with corners defined by those two cells.
冒号 ( :) 将范围内的所有单元格与由这两个单元格定义的角相加。
回答by DiGiMac
Treb, Matthieu's problem was caused by using Excel in a non-English language. In many language versions ";" is the correct separator. Even functions are translated (SUM can be SOMMA, SUMME or whatever depending on what language you work in). Excel will generally understand these differences and if a French-created workbook is opened by a Brazilian they will normally not have any problem. But VBAspeaks only US English so for those of us working in one (or more) foreign langauges, this can be a headache. You and CharlesB both gave answers that would have been OK for a US user but Mikko understod the REAL problem and gave the correct answer (which was also the correct one for me too - I'm a Brit working in Italy for a German-speaking company).
Treb,Matthieu 的问题是由于在非英语语言中使用 Excel 引起的。在许多语言版本中“;” 是正确的分隔符。甚至函数也会被翻译(SUM 可以是 SOMMA、SUMME 或其他任何内容,具体取决于您使用的语言)。Excel 通常会理解这些差异,如果法国人创建的工作簿由巴西人打开,他们通常不会有任何问题。但是VBA只会说美国英语,所以对于我们这些使用一种(或多种)外语工作的人来说,这可能会让人头疼。你和 CharlesB 都给出了对美国用户来说没问题的答案,但 Mikko 理解了真正的问题并给出了正确的答案(这对我来说也是正确的答案——我是一个在意大利工作的英国人,讲德语公司)。
回答by e.James
The correct character to use in this case is a full colon (:), not a semicolon (;).
在这种情况下使用的正确字符是完整的冒号 ( :),而不是分号 ( ;)。
回答by Treb
I don't know why, but if you use
我不知道为什么,但如果你使用
(...)Formula = "=SUM(D2,E2)"
(',' instead of ';'), it works.
(' ,' 而不是 ' ;'),它有效。
If you step through your sub in the VB script editor (F8), you can add Range("F2").Formulato the watch window and see what the formular looks like from a VB point of view. It seems that the formular shown in Excel itself is sometimes different from the formular that VB sees...
如果您在 VB 脚本编辑器 (F8) 中逐步执行您的子程序,您可以添加Range("F2").Formula到观察窗口并从 VB 的角度查看公式的外观。似乎Excel本身显示的公式有时与VB看到的公式不同......

