13

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 ?

0

1 Answer 1

22

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)"
7
  • So, do I understand you correctly, that using .FormulaLocal will make my code absolutely viable and not dependent on the languange system ? Commented Mar 1, 2016 at 13:48
  • Then why do we need the method .Formula whatsoever ?)) And why don't they just integrate the funcionality of .FormulaLocal into the method .Formula ?)) Strange enough Commented Mar 1, 2016 at 13:49
  • Yes, it should. All that is necessary would be to make sure that you are using either .FormulaLocal or .FormulaR1C1Local appropriately. The R12 reference in your sample was a little ambiguous; I hope I guessed correctly. Also, any quotes within the formula need to be doubled up as they are going to be within a quoted string. e.g. ="abc" becomes .FormulaLocal = "=""abc""".
    – user4039065
    Commented Mar 1, 2016 at 13:52
  • 3
    Not if your Office installation is RU-RU or anything but EN-US. However, myRange.Formula = "=ROW(mySheet!$12:$12)" will work on any system. (do not use Set to assign a formula)
    – user4039065
    Commented Mar 1, 2016 at 14:18
  • 1
    Thank you, thank you, thank you. I have struggled a lot with not get it to work with Formula. I have totally missed that it should be "," instead of ";". I usually look in VBA debugger for values, but not in a thousand years I could have imagine that Microsoft have this difference between Formula and FormulaLocal. I know they are strange, but this strange...
    – 244an
    Commented Nov 29, 2017 at 13:32

Not the answer you're looking for? Browse other questions tagged or ask your own question.