-->

VBA Set formula - getting Error 1004

2019-01-08 02:46发布

问题:

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 (I am not allowed to post pic's): http://i.imgur.com/qVSoDkv.png

I keep getting a 1004 Error when running the code. I can run the same code but with the SUM function instead:

"=SUM(B1:B5)"

This does not produce any errors. Thus I am suspecting the semicolons but have been unable to come up with a solution.

回答1:

.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.