简体   繁体   English

使用VBA在Excel工作表中插入公式的问题

[英]Problem with using VBA to insert formula in Excel sheet

I recently ran into an issue when using VBA to insert a formula into an Excel sheet.我最近在使用 VBA 将公式插入 Excel 工作表时遇到了一个问题。 Below the relevant piece of code;在相关代码段下方;

Calculatie_cell.Offset(n, 5) = "=SOM(H" & (n + 3 - 1) & ":H" & (n + 3 - Onderdelen_aantal) & ")"
Calculatie_cell.Offset(n, 8) = "=SOM(K" & (n + 3 - 1) & ":K" & (n + 3 - Onderdelen_aantal) & ")"

For the first iteration where it triggers, the expected output is;对于触发的第一次迭代,预期输出是;

=SOM(H4:H7)
=SOM(K4:K7)

The output I recieve is;我收到的输出是;

=@SOM(H4:H7)
=@SOM(K4:K7)

This output yields a #NAME?这个输出产生一个#NAME? error.错误。 Pressing F2, enter on the cell (like inputting it by hand), gives an error stating that this formula is not supported by older versions of Excel with the option to correct it to the expected output.按 F2,在单元格上输入(如手动输入),会出现错误,指出旧版本的 Excel 不支持此公式,并可选择将其更正为预期输出。 If I accept this it works.如果我接受它,它就可以工作。

I can't find why Excel thinks it needs the @ and the code below does not filter the @ from the formule (but it does filter it out of random text with an @)我不明白为什么 Excel 认为它需要 @ 并且下面的代码没有从公式中过滤 @ (但它确实从带有 @ 的随机文本中过滤掉了它)

Calculatie_cell.Offset(n, 5) = Replace(Calculatie_cell.Offset(n, 5), "@", "")
Calculatie_cell.Offset(n, 8) = Replace(Calculatie_cell.Offset(n, 8), "@", "")

Does anyone have a suggestion how I can get rid of the @, or another way to make the formula work?有没有人建议我如何摆脱@,或使公式起作用的另一种方法? Thanks in advance!提前致谢!

Relevant note;相关说明; I use Excel 2016 in Dutch, meaning "SOM" is the correct way to spell "SUM"我用荷兰语使用 Excel 2016,意思是“SOM”是拼写“SUM”的正确方法

User @Solar Mike was right.用户@Solar Mike 是对的。

Even though Excel wants the code to be "SOM" (Dutch spelling), in VBA it needs to ben SUM (English spelling).尽管 Excel 希望代码为“SOM”(荷兰语拼写),但在 VBA 中它需要 ben SU​​M(英语拼写)。 It auto-translates and works right of the bat.它会自动翻译并直接工作。 Thank you for your help!谢谢您的帮助!

If you post it as an answer, I will accept it as such.如果您将其发布为答案,我会接受它。

First of all, regardless of your Office installation language, VBA asks for engligh input.首先,无论您的 Office 安装语言是什么,VBA 都会要求输入英文。
Therefore, replace SOM by SUM in your code.因此,在您的代码中用SUM替换SOM
It should appear as SOM in the workbook, but not in the code.它应该在工作簿中显示为SOM ,而不是在代码中。

Second of all, using oCell = "=SOME_FORMULA" usually does not yield the intended result.其次,使用oCell = "=SOME_FORMULA"通常不会产生预期的结果。
Instead, one should use oCell.Formula2 = "=SOME_FORMULA" .相反,应该使用oCell.Formula2 = "=SOME_FORMULA"
This is why an @ shows up in the formula.这就是为什么@出现在公式中的原因。

See this article for information.有关信息,请参阅文章。


Finally, your code should be最后,你的代码应该是

Calculatie_cell.Offset(n, 5).Formula2 = "=SUM(H" & (n + 3 - 1) & ":H" & (n + 3 - Onderdelen_aantal) & ")"
Calculatie_cell.Offset(n, 8).Formula2 = "=SUM(K" & (n + 3 - 1) & ":K" & (n + 3 - Onderdelen_aantal) & ")"

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

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