How to change VBA array decimal separator?

血红的双手。 提交于 2020-01-07 09:21:51

问题


I would like to change VBA array decimal separator to dot. I see it as comma. I tried: Application.DecimalSeparator="."

But when I get the value as MyString = matrix(2, 1), the decimal separator in VBA arrays maliciously persists as comma. I am not able to get rid of the pest.

Is there a way to detect which system separator for VBA arrays is used?


回答1:


VBA uses quite a few bits drawn from various parts of the platform to work out which decimal and thousands separator to use. Application.DecimalSeparator changes a few instances (mostly on the workbook); you can tweak others at the OS level, but even then though you get to a couple of cases where you can't change the settings.

Your best bet is to write a simple function to check which separator your platform uses based on a trial conversion of say 1.2 to a string and see what the second character ends up being. Crude but strangely beautiful.

Armed with that you can force an interchange of . and , as appropriate. Naturally then though you will have to manage all string to number parsing yourself, with some care.

Personally though I think this is epitomises an unnecessary fight with your system settings. Therefore I would leave everything as it is; grin and bear it in other words.




回答2:


You have to change it in system settings, Excel takes this kind of settings from system.




回答3:


I have end up with this function which does exactly what I want. Thank you all for answers, comments and hints.

Function GetVBAdecimalSep()
    Dim a(0) As Variant
    a(0) = 1 / 2
    GetVBAdecimalSep = Mid(a(0), 2, 1)
End Function


来源:https://stackoverflow.com/questions/47079257/how-to-change-vba-array-decimal-separator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!