Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does casting to double using "String * 1" fail? Will CDbl(String) work on all systems?

Tags:

excel

vba

I have an application which contains the line below to assign a parsed XML value to a variant array.

V(2) = latNode.Text * 1

This works fine on my system (Windows 7, Excel 2010) but doesn't work on some other system or systems - and I've not been able to get a response from the user who reported the problem.

I've switched out the offending line for:

V(2) = CDbl(latNode.Text)

This still works on my system, but then I had no problem in the first place. The question is on what systems does the first approach fail and why, and will the second method always work? I'm sure I've used the "String * 1" trick elsewhere before and would like to know how concerned I should be about tracking down other occurrences.

Thanks.

like image 598
Jamie Bull Avatar asked Dec 14 '25 17:12

Jamie Bull


1 Answers

Maybe it's related to thousands separator and decimal mark. Office VBA uses cultural settings even in CDbl, in my German Excel version, it's reversed compared to English, CDbl("123.4") is parsed to 1234, CDbl("123,4") to 123.4.

Val(x) will always parse the dot as decimal mark.

like image 72
KekuSemau Avatar answered Dec 18 '25 17:12

KekuSemau



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!