Write a formula in an Excel Cell using VBA - vba

I'm trying to use VBA to write a formula into a cell in Excel.
My problem is that when I use a semicolon (;) in my formula, I get an error:
Run-time error 1004
My macro is the following :
Sub Jours_ouvres()
Dim Feuille_Document As String
Feuille_Document = "DOCUMENT"
Application.Worksheets(Feuille_Document).Range("F2").Formula = "=SUM(D2;E2)"
End Sub

You can try using FormulaLocal property instead of Formula. Then the semicolon should work.

The correct character to use in this case is a full colon (:), not a semicolon (;).

The correct character (comma or colon) depends on the purpose.
Comma (,) will sum only the two cells in question.
Colon (:) will sum all the cells within the range with corners defined by those two cells.

Treb, Matthieu's problem was caused by using Excel in a non-English language. In many language versions ";" is the correct separator. Even functions are translated (SUM can be SOMMA, SUMME or whatever depending on what language you work in). Excel will generally understand these differences and if a French-created workbook is opened by a Brazilian they will normally not have any problem.
But VBA speaks only US English so for those of us working in one (or more) foreign langauges, this can be a headache.
You and CharlesB both gave answers that would have been OK for a US user but Mikko understod the REAL problem and gave the correct answer (which was also the correct one for me too - I'm a Brit working in Italy for a German-speaking company).

I don't know why, but if you use
(...)Formula = "=SUM(D2,E2)"
(',' instead of ';'), it works.
If you step through your sub in the VB script editor (F8), you can add Range("F2").Formula to the watch window and see what the formular looks like from a VB point of view. It seems that the formular shown in Excel itself is sometimes different from the formular that VB sees...

Related

Error 1004 when inserting formula into cell using macro

I'm trying to use a macro to insert an equation into a cell. The equation works fine if I copy it in myself but I need to copy it to 6000 cells in each or four worksheets. This question seems pretty common, but the usual answer of replace ";" with "," doesn't apply. The first line catches error 1004.
Range("J1").FormulaLocal = "=IF(ISERROR(F1),"",IF(ISTEXT(F1),"",F1))"
Range("J1:J6000").FillDown
I also tried using .formulaLocal but that doesn't seem to help.
You need to use double quotes to leave one quote:
Range("J1").FormulaLocal = "=IF(ISERROR(F1),"""",IF(ISTEXT(F1),"""",F1))"

Named ranges won't work

Whenever I try to make a named range in Excel I keep getting an error. I believe my formula is correct:
=OFFSET($B$2,0,0,COUNTA($B$2:$B$200),1)
However when I press OK I keep getting the dialog screen which states Excel found a problem with my formula.
Then it highlights the following part of the formula: $B$2,0,0,COUNTA.
I looked through various tutorials where this formula should be correct.
Can someone help me out on this?
One way to see this error message is if you are using the incorrect argument separators. For example, many locale's use the semicolon ;.
Try replacing the commas in your formula with semicolons
=OFFSET($B$2;0;0;COUNTA($B$2:$B$200);1)
or whatever your locale argument separator is.
Thanks, it were indeed the seperators which caused the problem: I have to use ; instead of a , - this tricked me as all of the tutorials I watched used the comma as well.

Formula R1C1 issues using IF

this is new for me. I have an issue trying to run a formula in VBA using IF.
The idea is to include in AR3 column a modification date when in C3 there contains information.
This is what I wrote:
Range("AR3").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-3]="";"";IF(RC[-44]="";NOW();RC[-44])"
Thanks in advance!!
A couple of things are not right with your formula:
You need to escape the double quotes (with double quotes... so four in total)
You are missing a closing parenthesis
This is optional, but you don't need to select the range
You can use the following corrected statement:
Range("AR3").FormulaR1C1 = "=IF(RC[-3]="""","""",IF(RC[-44]="""",NOW(),RC[-44]))"
Next time you run into this kind of trouble, just use the macro recorder: click on "Record macro", and enter your formula. Plus, you can always change the display in Excel to the R1C1 Reference style (Options > Formulas > R1C1 reference style) when that is helpful (Personally I've mapped this change to a shortcut key combination so I can easily get column numbers if needed)

Range(...).Formula does not translate fully

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

Removing invisible question mark from text - #​E using vba

I have to read the text from the cells of a column in excel and search for it in another sheet.
say for example, the text in sheet1 column A is "Evoked Potential Amplitude N2 - P2." This has to be searched in sheet2 column C. This fails because a question mark appears before the "E" which is not present in the value in the sheet2.
Both are representation of same character in different application. Maybe someone might recognize it.
In the excel sheet I don't see any junk characters, but while handling it in the vb code I see a question mark before the word - Evoke.
This data was extracted from a share point application and this character (?) is not visible to the plain eye. Search and replace functions are not working in this case.
Unicode 8203 is a zero-width space. I'm not sure where it's coming from. It is probably a flaw in the way the data is imported into Excel which you haven't noticed before, but it might be worth fixing.
In the meantime, you can simply use the Mid() function in Excel VBA to remove the unwanted character. For example instead of
x = cells(1,1).value
use
x = Mid(cells(1,1).value,2)
which deletes the first character.