Excel - Inserting formula with VBA - vba

Hello Stackoverflow friends,
I am struggling for 1 hour with a formula I would like to insert via VBA:
Formula = "=IFERROR(VLOOKUP(Q" & j & ";Table1[#All];2;FALSE);"""")"
ThisWorkbook.Worksheets("Sheet1").Cells(j, "AE").FormulaArray = Formula
I get the following error message:
Run-time error '1004' - Application-defined or object-definied error
Is there an issue with the brackets or double quotes?
Thanks!

Replace the semicolons with commas:
Formula = "=IFERROR(VLOOKUP(Q" & j & ",Table1[#All],2,FALSE),"""")"
OpenOffice uses semicolons to separate function parameters, Excel normally uses commas, and always uses commas when setting formulas in the above fashion.

When programming in any lanugage also in VBA - better not tied up user to specific regional settings or specific excel version.
So instead of this:
Formula = "=IFERROR(VLOOKUP(Q" & j & ";Table1[#All];2;FALSE);"""")"
ThisWorkbook.Worksheets("Sheet1").Cells(j, "AE").FormulaArray = Formula
Better use this approach, when you determine exact user environment:
s = Application.International(xlListSeparator)
Formula = "=IFERROR(VLOOKUP(Q" & j & s +"Table1[#All]" + s + "2" + s + "FALSE)" + s + """"")"
ThisWorkbook.Worksheets("Sheet1").Cells(j, "AE").FormulaArray = Formula
p.s. I didn't checked the formula for the brackets etc. but just indicating the correct usage of list separator, and how to insert formulas with VBA code within cells in correct way.
As well, as previous post says - excel probably change the formula automatically when you open it. However excel do not change VBA code automatically, so be aware and pay attention to proper code in VBA.

Depending on the regional settings, the list separator (which is also used to separate parameters in functions) is either the semicolon or the comma. This applies when typing a formula into a cell.
Excel dynamically adjusts the list separator (and function names) according to the regional settings of the current computer when a file is opened.
So, if a user with German regional setting, which have the list separator ; saves a file, then a user with US regional settings and a list separator , opens the same file, Excel will adjust the German list separators in the formulas automatically.
When writing VBA, though, you will always need to use the US-English conventions for the list separator, which is the comma.

Related

SQL & Excel - Storing excel functions in SQL

I am storing vlookups in a sql field. When I load it into excel:
that code is showing up with a ' (single quote) in front of the code.
Is it possible to store a function in a SQL field that will load into Excel without the ' (single quote) at the front of the string so that when opened in excel the function works?
A single quote as the first character in cell, forces text format for the column at the start of a cell is used to force text format. You may have your cells formatted as text.
You may also need to do Data-> Text to Columns-> Finish.
Also, try hitting 'Calculate Now' in the formulas tab.

Error in formula with predefined variable

I want to add a formula for this range inside my macro and include inside it the variable percentage that I have set in the beginning of the macro. When I run it, it says there is an error "Application or object defined error" in this line. The entire macros runs perfectly, but today I want to add this line and it wont, so obviously the error is in the syntax of the formula. I am providing it below.
ws3.Range("C9:C12").Formula = "=IFERROR(B9/(1- & percentage),"""")"
Everything inside the quotes will appear as a literal string so you will get the word "percentage" appearing in your formula. The spreadsheet doesn't know what this is as you have defined it in your macro (there is no doubt a proper technical term for this).
Amended as per #Peh's suggestion.
ws3.Range("C9:C12").Formula = "=IFERROR(B9/(" & 1-percentage & "),"""")"

Different languages issue when inserting formula from VBA

do I understand correctly, that if I use a command like
Set myRange.formula = “=ROW(mySheet!R12)”
my macro will cause #NAME? error appear in cells if it is run on, say, Russian Excel.
I mean that in this case the above formula should be hard-coded like
Set myRange.formula = “=СТРОКА(mySheet!R12)”
where СТРОКА is the Russian analogue of the SUM function. I wouldn't anticipate Excel to be smart enough to translate the formulas in run-time.
So is there any way around this and, most importantly, what is the most generic code to make the macro work correctly irrespective of languange ?
VBA is very EN-US-centric. VBA's .Formula and .FormulaR1C1 expect the ROW function. To use regional language function 'flavors' like СТРОКА then the Range.FormulaLocal property or Range.FormulaR1C1Local property should be employed instead.
The same holds true for list separator characters. Use a comma (e.g. ,) to separate the arguments in a function when using .Formula or .FormulaR1C1 regardless of system regional settings. If your system uses a semi-colon (e.g. ;) as the list separator character, this should only be used with .FormulaLocal or .FormulaR1C1Local.
The result on the worksheet will properly reflect the language settings of the Office installation.
myRange.Formula = "=ROW(mySheet!$12:$12)"
myRange.FormulaR1C1 = "=ROW(mySheet!R12)"
myRange.FormulaLocal = "=СТРОКА(mySheet!$12:$12)"
myRange.FormulaR1C1Local= "=СТРОКА(mySheet!R12)"

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)

Write a formula in an Excel Cell using 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...