如何设置打印到与系统设置不同的文件的小数点分隔符
how to set the decimal separator for printing to file different from the system setting
在 Excel 2010 中使用 VBA 我想将工作表中单元格的内容打印到文件中,该单元格是一个十进制数。为此,我正在使用“Print #”语句。我在一个环境中工作,其中逗号“,”是系统范围的小数点分隔符。对于打印输出,我希望点“.”成为小数点分隔符。
以下代码不起作用:
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
Print # 语句的 documentation 表示:
All data written to the file using Print # is internationally aware;
that is, the data is properly formatted using the appropriate decimal
separator.
当我在 Windows 7 Enterprise Control Panel 中更改系统范围的设置时,我得到了所需的输出,但这不是我想要的。
因为你使用的是宏,你可以直接控制输出到文本文件的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
使用 .Text 属性 允许您获得单元格的值所见即所得。剩下的只是字符串操作。
编辑#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
在 Excel 2010 中使用 VBA 我想将工作表中单元格的内容打印到文件中,该单元格是一个十进制数。为此,我正在使用“Print #”语句。我在一个环境中工作,其中逗号“,”是系统范围的小数点分隔符。对于打印输出,我希望点“.”成为小数点分隔符。
以下代码不起作用:
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
Print # 语句的 documentation 表示:
All data written to the file using Print # is internationally aware; that is, the data is properly formatted using the appropriate decimal separator.
当我在 Windows 7 Enterprise Control Panel 中更改系统范围的设置时,我得到了所需的输出,但这不是我想要的。
因为你使用的是宏,你可以直接控制输出到文本文件的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
使用 .Text 属性 允许您获得单元格的值所见即所得。剩下的只是字符串操作。
编辑#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