本文介绍了如何在 VBA Sub 中应用 SumIf 公式?获取错误 1004的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个在单元格中放置 SUMIF 公式的 Sub.我已将问题简化为一个简单的设置:

I am trying to create a Sub that places a SUMIF formula in a cell. I have reduced the problem to a simple setup:

Private Sub CommandButton1_Click()
Cells(2, 3).Formula = "=SUMIF(A1:A5;D1;B1:B5)"
End Sub

我收到一个 1004 错误.

我可以运行相同的代码,但使用 SUM 函数:

I can run the same code but with the SUM function instead:

"=SUM(B1:B5)"

这不会产生错误.因此,我怀疑公式中的分号.

This does not produce an error. Thus I suspect the semicolons in my formula.

推荐答案

.Formula 接受英文公式.
英文的参数分隔符是,.

如果要使用 Excel 安装语言的公式,请使用 .FormulaLocal.

If you want to use formulas in the language of your Excel installation, use .FormulaLocal.

但是,为了确保您的代码可以在任何 Excel 上运行,请修复您的公式以符合 en-us 区域设置.

However to ensure your code will run on any Excel, fix your formula to be in accordance with the en-us locale.

这篇关于如何在 VBA Sub 中应用 SumIf 公式?获取错误 1004的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 14:58