gpt4 book ai didi

vba - 如何设置打印到文件的小数点分隔符与系统设置不同

转载 作者:行者123 更新时间:2023-12-04 22:03:19 28 4
gpt4 key购买 nike

在 Excel 2010 中使用 VBA 我想将工作表中的单元格的内容打印到文件中,该单元格是十进制数字。我为此使用“打印#”语句。我在逗号“,”是系统范围的小数分隔符的环境中工作。对于打印输出,我希望点“。”作为小数分隔符。
以下代码不起作用:

Public Sub printDecimal()
Application.UseSystemSeparators = False
Application.DecimalSeparator = "."
Open "testfile.txt" For Output As #1
Print #1, ActiveSheet.Range("A1").Value
Close #1
Application.UseSystemSeparators = True
End Sub
documentation对于 Print # 声明说:

All data written to the file using Print # is internationally aware;that is, the data is properly formatted using the appropriate decimalseparator.


当我在 Windows 7 企业控制面板中更改系统范围的设置时,我得到了所需的输出,但这不是我想要的。

最佳答案

由于您使用的是宏,因此您可以直接控制输出到文本文件的 Material :

Public Sub printDecimal()
Dim st As String
Close #1
Open "C:\TestFolder\testfile.txt" For Output As #1
st = Range("A1").Text
st = Replace(st, ",", ".")
Print #1, st
Close #1
End Sub

使用 .文本 属性允许您获取单元格的值所见即所得。剩下的只是字符串操作。

编辑#1:

发布代码不起作用的原因是应用 Application.DecimalSeparator 类似于应用格式......基础值不会改变。运行这个看看有什么不同:
Sub demo()
Dim st As String, st2 As String
Application.UseSystemSeparators = False
Application.DecimalSeparator = "|"
Close #1
Open "C:\TestFolder\testfile2.txt" For Output As #1
Print #1, ActiveSheet.Range("A1").Value & vbCrLf & ActiveSheet.Range("A1").Text
Close #1
Application.UseSystemSeparators = True
End Sub

关于vba - 如何设置打印到文件的小数点分隔符与系统设置不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30167265/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com