Home » excel » excel – Range(…).Formula does not translate fully

excel – Range(…).Formula does not translate fully

Posted by: admin May 14, 2020 Leave a comment

Questions:

I cannot figure this one out.

We use mostly french-version Excel (as we live in a french-speaking province of Canada). Somewhere in VBA code I set a cell’s formula directly. Normally, we have to write the formula in english and Excel does the translation (writing the formula in any other language than english in VBA results in an error as far as I know). However, only HALF of this equation is translated which I think is causing me issues (writing the correct formula in another cell yields different results and most probably right results).

range("J2").Formula = "=round(IF(F2="",0,F2),2)-round(IF(G2="",0,G2),2)"

Is translated to this in the cell:

=ARRONDI(SI(F2=",0,F2),2)-round(IF(G2=",0,G2),2)

As you can see, the right part should read “ARRONDI(SI(..” but it does not read that way. I have tried adding spaces, removing the minus sign altogether, etc. Nothing works, it’s always half translated. Any idea ?

How to&Answers:

In VBA you neexd to escape your quotations like this:

range("J2").Formula = "=round(IF(F2="""",0,F2),2)-round(IF(G2="""",0,G2),2)"

This is because the " Character is used in VBA as the start / end of a string. So if you want ot include it IN a string you need to type it twice in a row.