vba 为什么VBA会自动将十进制更改为逗号?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43863482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 12:34:06  来源:igfitidea点击:

Why is VBA changing decimal to comma automatically?

excelvbadecimalcomma

提问by Dave

I have an Excel macro in VBA. Yesterday everything worked fine, this morning VBA is changing a decimal point to a comma. That is from 5.1 to 5,1.

我在 VBA 中有一个 Excel 宏。昨天一切正常,今天早上 VBA 将小数点更改为逗号。即从 5.1 到 5,1。

I am using a German system and have set in Excel's advanced options that a point is a decimal and comma is thousands.

我使用的是德国系统,并在 Excel 的高级选项中设置了一个点是小数点,逗号是千位。

For example I have a value for daily scrum meeting in the Excel sheet set at 3.75

例如,我在 Excel 工作表中将每日 Scrum 会议的值设置为 3.75

Excel sheet input:
screenshot

Excel表格输入:
截屏

when I use a function to read in the value such as:

当我使用函数读取值时,例如:

Sub TestFunction()
    MsgBox (Sheets("Input_Parameters").Cells(25, 2))
End Sub

I get the value with commas.

我用逗号得到值。

VBA function output:
output

VBA 函数输出:
输出

Is there a setting in the VBA environment to fix this or is there something I am missing. All my backups of the macros are showing the same affect so I know it is nothing I have changed in the code in today's version.

VBA 环境中是否有设置可以解决此问题,或者我遗漏了什么。我所有的宏备份都显示出相同的影响,所以我知道我在今天版本的代码中没有任何改变。

回答by P??

This is a normal behavior on German systems with German default locale Windows settings (comma as decimal separator and point as thousand separator in Windows settings).

这是在德语系统上使用德语默认区域设置 Windows 设置(在 Windows 设置中逗号作为小数分隔符和点作为千位分隔符)的正常行为。

MsgBox .Cells(25, 2).Value

returns the value with the format of the Windows locale default.

返回具有 Windows 语言环境默认格式的值。

The Application.DecimalSeparatorthat you set in your Excel options affects what is displayed in the cell, but not what is displayed by message boxes.

Application.DecimalSeparator您在Excel选项设置影响是什么显示在单元格,但不是由消息框显示。

Therefore you can use

因此你可以使用

MsgBox .Cells(25, 2).Text

which returns the value as text formatted like in the cell.

它将值作为文本格式返回到单元格中。



Another workaround is to replace the commas with replace()function:

另一种解决方法是用replace()函数替换逗号:

MsgBox Replace(.Cells(25, 2).Value, ",", ".")

回答by Vityata

One of the first functions I have created, after I came to Germany was this one:

来到德国后,我创建的第一个功能之一是:

Public Function ChangeCommas(ByVal myValue As Variant) As String

    Dim temp As String
    temp = CStr(myValue)
    ChangeCommas = Replace(temp, ",", ".")

End Function

Since the last 2 years it is present in every VBA project.

自过去 2 年以来,它出现在每个 VBA 项目中。