Insert a formula into a specific cell using VBA [duplicate] - excel

When a user manually enters formula in a sheet in the English version of Excel and sends this document to another user who has Excel in another version, the formula is automatically translated to the language in which the document is opened.
'Formula entered in Sheet1!B1 in the English Excel version
=VLOOKUP(A1,Sheet2!$A$1:$C$150,3,FALSE)
'Formula as shown in a Dutch Excel version
=VERT.ZOEKEN(A1;Sheet2!$A$1:$C$150;3;ONWAAR)
When I programmatically add a formula, in my English version, it would look like this:
Sub AddFormula()
ThisWorkbook.Worksheets("Sheet1").Cells(1, 2).Formula = "=VLOOKUP(A1,Sheet2!$A$1:$C$150,3,FALSE)"
End Sub
If I were to run this procedure on my English Version, then save the document and send it over to a user with another language, it would be fine as the formula is in the cell and thus translated to whatever language and locale the 2nd user uses.
My question is what happens when the 2nd user runs the procedure, in the Dutch (or other language for that matter) Excel with the formula in the code written in the EN-US format. Will it be translated or will it throw an error and in case of the latter, how would I solve that and make the formula insertion language proof?

In Excel vba the .Formula requires that the formula to be inserted be in EN-USA format. This is regardless of the local settings.
Excel will then translate it into the local vernacular when putting it in the sheet.
So no matter what the local settings it will work cross languages.
VBA also has the .FormulaLocal which allows the entry of the formula in the native language. But (as far as I know) it will not work cross different local settings.

Related

Excel TEXT() Formula Getting the Month returning "mmm"

I got a report in excel and I'm having some trouble with a Russian PC. The formula TEXT is being used to get the month name from a date but, in the Russian PC it's not working and not given an error.
The formula is =TEXT(D7, "mmm") and the result is "mmm".
I also tried (The excel way to handle dates in different languages):
=TEXT(D7, "[$-409]mmm") and the result is "mmm".
I've tested different date functions and they all working. (Sum(), month(), changing data format, etc).
Version: Excel 365
As it works in my pc, does anyone have any idea what might be causing the error?
First - The mmm should be MMM.
Then, the fact that the PC is in Russian is a bit irrelevant. What matters is the installation language of Excel.
You may try the following
write 43319 on range A1;
then write this formula =TEXT(A1,"MMM"), using the English M and not the Cyrillic ones. Although they look quite the same, they are different.
it should return Aug as the month;
I've found a way to fix it creating a new formula in VBA and it worked great.
Press Alt+F11 (to open the VBA editor) Then Click the menu item Insert > Module In the new VBA module, enter the following:
Public Function FMT$(ByVal Value, ByVal strFormat)
FMT = VBA.Format$(Value, strFormat)
End Function
To use this, simply type =FMT(A1, "MMM") instead of =TEXT(A1, "MMM").
Credits to https://superuser.com/questions/730371/how-to-prevent-excel-to-use-the-os-regional-settings-for-date-patterns-in-formul

Does Range.Formula property translate to other locale

When a user manually enters formula in a sheet in the English version of Excel and sends this document to another user who has Excel in another version, the formula is automatically translated to the language in which the document is opened.
'Formula entered in Sheet1!B1 in the English Excel version
=VLOOKUP(A1,Sheet2!$A$1:$C$150,3,FALSE)
'Formula as shown in a Dutch Excel version
=VERT.ZOEKEN(A1;Sheet2!$A$1:$C$150;3;ONWAAR)
When I programmatically add a formula, in my English version, it would look like this:
Sub AddFormula()
ThisWorkbook.Worksheets("Sheet1").Cells(1, 2).Formula = "=VLOOKUP(A1,Sheet2!$A$1:$C$150,3,FALSE)"
End Sub
If I were to run this procedure on my English Version, then save the document and send it over to a user with another language, it would be fine as the formula is in the cell and thus translated to whatever language and locale the 2nd user uses.
My question is what happens when the 2nd user runs the procedure, in the Dutch (or other language for that matter) Excel with the formula in the code written in the EN-US format. Will it be translated or will it throw an error and in case of the latter, how would I solve that and make the formula insertion language proof?
In Excel vba the .Formula requires that the formula to be inserted be in EN-USA format. This is regardless of the local settings.
Excel will then translate it into the local vernacular when putting it in the sheet.
So no matter what the local settings it will work cross languages.
VBA also has the .FormulaLocal which allows the entry of the formula in the native language. But (as far as I know) it will not work cross different local settings.

Name of Excel formula in different languages in xlsx format

I generated Excel file in my application in xlsx format. Unfortunately if I use English name of the function and my Excel is not set to English then Excel identifies it as bad function name. Simillar situation does not happen in xls format.
Is it any way to avoid this problem?
Configuration of Excel itself is not acceptable solution as client should not be forced to make changes in his environment.
Thanks in advance for any help
Excel has its own way for translating the formulas from every language to English. You should not take care of that, it does it quite well. Thus a file in Spanish Excel can be read in French Excel without further adjustments.
E.g., see the German formula "SUMME" in F1. It is translated by excel to "SUM", when you ask about it -> see the immediate window on the right.
The only thing you should avoid is formula like this:
activecell.FormulaR1C1Local= "=SUMME(Z(8)S(-2):Z(9)S(-2))", because it takes the local formula as a string and Excel cannot find a way to translate "SUMME" to "SUM".

Groovy POI created cells considered blank by other programs until ENTER is manually pressed in cells

Ok this is kind of hard to explain.
I create a cell in my worksheet using JAVA POI
newCell= row.getCell(index)
if (containerCell == null) {
containerCell = row.createCell(index)
}
newCell.setCellType(HSSFCell.CELL_TYPE_STRING)
newCell.setCellValue(strVar)
If i then open the worksheet in EXCEL, I see that the cell value is indeed set. However if I load this into another external program that reads EXCEL sheets, it claims the cell I just set is blank.
NOW, if I go back into excel and do a simple "Hit return" on the cell in question, in the formula bar (even though its not a formula) and try to reload it into the external program, it works fine. Do I need to evaluate a formula on a string?
Thanks
I have no experience with Java, but having to hit enter in a cell is usually indicative of calculation being set to manual. Since you are working with API it may be treating your input from Java as a formula (?).
Open the offending workbook in its broken state, and on the sheet you are having problems with choose [Formulas] tab at top, then [Calculation]>Calculate Sheet.
If this updates the value then I see two options...
See if there is an option to manually calculate the sheet with POI, after the value has been entered.
Alternatively, you can write just a tiny bit of VBA to force calculation on that sheet when workbook is opened. MSDN offers a simple example.
http://msdn.microsoft.com/en-us/library/office/aa223802%28v=office.11%29.aspx
I have had to do this for Excel apps where calculation had to be set to manual to avoid excessive overhead. Just a guess though..
I am not sure why this was happening.. and evaluating the individual cells did not solve it however running evaluteAll() on the entire workbook made it work!

Formulas within Named Range changes when opened in foreign language version of Excel

When opened in an English language version of Excel, the following definition for a named range works just fine:
=OFFSET('Summary Data'!$B$9,0,0,TotalStops+1,SummaryDataColumns)
However, when the same worksheet (created using an English language version of Excel) is open using a Portuguese language version of Excel the formula in the named range is changed to:
=DESLOC('Summary Data'!$B$9;0;0;TotalStops+1;SummaryDataColumns)
To my understanding "DESLOC" is the correct translation for OFFSET, but all of commas in the formula have been replaced with semi-colons. Now anytime that the named range is referred to in vba code, the code execution fails at that point.
Just for reference, the call that references the named range is of the form:
thisworkbook.Names("dbSummaryData").RefersToRange
This link may provide a better explanation, Find the current user language
You should be able to put a check into your VBA code on what language is selected and if its other than English it will swap semi-colon for comma then execute.

Resources