Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.8k views
in Technique[技术] by (71.8m points)

excel - How do I apply a SumIf Formula in a VBA Sub? Getting Error 1004

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

Picture of the spreadsheet [http://i.imgur.com/qVSoDkvm.png]

I get a 1004 Error.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

.Formula accepts formulas in English.
Parameter separator in English is ,.

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...