如何确保 Excel 不会更改其他语言版本中数据标签的数字格式?

How can I make sure Excel does not change number format of a data label in other language versions?

我有一个工作簿,其中包含 VBA 代码,用于指定水平条形图中数据标签的数字格式。

Chart.SeriesCollection(1).DataLabels.NumberFormat = "0.0"

在我的 Excel (O365) 英文副本中,output is as expected, e.g. 3.7. When the workbook is opened in another copy of Excel (O365) in a language other than English, Excel appears to insert a backwards slash that can be seen in the Format Code -field of the Format Data Labels -sidebar (i.e. 0\.0). Where output should be e.g. 3.7, output becomes 0.4

当我在被非英语语言触及后加载工作簿时,问题仍然存在 Excel - 我的版本没有去掉反斜杠。当我删除反斜杠时,问题消失了,输出再次符合预期。

在我看来 Excel 将前导零和小数点视为纯文本,将第二个零视为预期的数字字符。

当以不同语言版本打开工作簿时,如何确保 Excel 保持 VBA 中指定的数字格式?

没有直接的解决方案,但如果 NumberFormat 仍然存在这种情况,您可以通过编程方式使用内置 Excel 函数的组合来获得相同的数字。

例如:

number = Round(number, 1)

在这种情况下,您可以将 NumberFormat 保留为 "General" 或类似的格式

看来最简单的解决方案是以编程方式禁用小数点分隔符并用您自己的分隔符替换它们,as described here

Application.DecimalSeparator = "."
Application.ThousandsSeparator = ","
Application.UseSystemSeparators = False

我在搜索如何确定 Excel 的语言版本时遇到了这个解决方案 in this thread

另一种解决方法是在定义数字格式时使用系统规定的小数点分隔符。该解决方案似乎也产生了所需的结果(显示一位小数,即使为 0),尽管使用系统定义的任何小数点分隔符:

Chart.SeriesCollection(1).DataLabels.NumberFormat = "0" & Application.International(xlDecimalSeparator) & "0"