VBA CSng(字符串)格式错误 [英] VBA CSng(string) wrong format

查看:164
本文介绍了VBA CSng(字符串)格式错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从xml文件中读取了一个节点,并尝试将其属性之一解析为一个.

I read in a node from a xml file and try to parse one of its attributes to a single.

Dim x_coord_single As Single
Dim x_coord_string As String
x_coord_string = node.Attributes.getNamedItem("x_coord").Text
x_coord_single = CSng(x_coord_string )

我分配x_coord_string后等于"9.0647"
但是CSng函数返回90647

After i assign x_coord_string it equals "9.0647"
But the CSng function returns 90647

我希望x_coord_single9.0647.

我直接尝试了CSng("9.0647"),但是结果相同.

I tried CSng("9.0647") directly but it's the same outcome.

关于不是为什么的任何建议吗?
如果正在影响MS Access 2010,我正在使用它.

Any suggestions on why it is not?
I'm working with MS Access 2010 if it affects this anyhow.

推荐答案

尝试如下:

Public Function fnStrChangeCommas(ByVal myValue As Variant) As String
    fnStrChangeCommas = Replace(CStr(myValue), ".", ",")
End Function

x_coord_single = CSng(fnStrChangeCommas(x_coord_string ))

它应该起作用,因为您的语言区域设置使用,作为小数点分隔符,而VBEditor使用..

It should work, because your language regional settings use , as a decimal separator and the VBEditor uses the ..

要查看您的系统在VBA中使用的分隔符,请运行以下代码:

To see the separators your system is using in VBA, run the following code:

Public Sub TestMe()

    Debug.Print "Decimal separator: "; Application.DecimalSeparator
    Debug.Print "Thousands separator: "; Application.ThousandsSeparator

End Sub

如果返回此值:

Decimal separator: ,
Thousands separator: .

然后您可以考虑使用fnStrChangeCommas函数.

Then you may consider using the fnStrChangeCommas function.

这篇关于VBA CSng(字符串)格式错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆