I had the impression that IsNumeric(MyString) would reveal if Val(MyString) is expected to fail or not. I have found unexpected differences depending on regional settings.
Example 1, Swedish regional settings (uses , as decimal separator):
IsNumeric("1.1")=> TrueVal("1.1")=> 1.1IsNumeric("1,1")=> TrueVal("1,1")=> 1
Example 2, Estonian regional settings (also uses , as decimal separator):
IsNumeric("1.1")=> FalseVal("1.1")=> 1.1IsNumeric("1,1")=> TrueVal("1,1")=> 1
My typical generic code for converting as string into a number is:
Function ConvertToNumber(MyNumber as String) as Double
If IsNumeric(MyNumber) then
ConvertToNumber = Val(Replace(MyNumber, ",", "."))
Else
MsgBox "Invalid format!"
End If
End Function
But this failed unexpectedly in Estonian regional settings. Any idea if this is the intended behavior? Microsoft explanations on what IsNumber is doing is also a bit vague. What do you suggest to use instead? If Val(MyNumber & "1")<>0? This would fail for some special cases such as 0E+0. One could also consider catching any error from:
CDbl(Replace(MyString, ".", Application.International(xlDecimalSeparator))
I could use regular expressions, but there must be better ways.
I would appreciate input on this.
/Jonas
