简体   繁体   English

使用宏在Excel中更改小数点分隔符

[英]Changing decimal separator in Excel using a macro

I am using a simple java program that outputs data into several comma separated text files. 我正在使用一个简单的Java程序,该程序将数据输出到几个逗号分隔的文本文件中。 I import these files to Excel using a macro pasting each file into separate sheets named after file name. 我使用宏将这些文件导入Excel,将每个文件粘贴到以文件名命名的单独工作表中。 The problem is that the output decimal separator is point "." 问题在于输出的十进制分隔符是点“”。 and the regional setting is comma ",". 并且区域设置为逗号“,”。 I don't want to keep changing the regional settings all the time. 我不想一直更改区域设置。

Using Ctrl+F and replacing all points with commas works fine. 使用Ctrl + F并将所有点替换为逗号都可以正常工作。 But if I record this as a macro and run it the data is displayed incorrectly and I get "number stored as text" errors (like this example ). 但是,如果我将其记录为宏并运行,则数据显示不正确,并且出现“数字存储为文本”错误(如本示例 )。 I have been looking at two possible solutions for this: 我一直在寻找两种可能的解决方案:

  1. Using a replace function like here . 使用此处的替换功能。 But I have trouble getting it to run as I don't understand what some of the lines do and what I need to change. 但是我无法使其运行,因为我不了解某些生产线的功能以及需要更改的内容。

  2. Changing the comma separator settings in Excel like here . 这里一样在Excel中更改逗号分隔符设置。 This solution would require these settings to reset on exit. 此解决方案将要求这些设置在退出时重置。 Doing it this way does not really solve the problem but goes around it. 这样做并不能真正解决问题,但是可以解决它。

Example output here . 示例输出在这里 Thank you very much for any input! 非常感谢您的投入!

If you use the Open command in Excel you can open that, modify the advanced button in the screen "Column Data Format" (Step 3 of 3) and modify the import separator. 如果您在Excel中使用“打开”命令,则可以打开它,修改“列数据格式”屏幕中的高级按钮(第3步,共3步),然后修改导入分隔符。
If you use a macro use the following: 如果使用宏,请使用以下命令:

Workbooks.OpenText Filename:="E:\0\aa.TXT", Origin:=xlMSDOS, StartRow:=1 _
    , DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
    ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, Comma:=False _
    , Space:=False, Other:=False, FieldInfo:=Array(Array(1, 1), Array(2, 1)), _
    DecimalSeparator:=".", ThousandsSeparator:="&", TrailingMinusNumbers:= _
    True

If you don't select the Thousands separator, you can have some strange value like 2.345 it's imported like 2345. (The same with open). 如果不选择“千位分隔符”,则可能会有一些奇怪的值,如2.345,它的导入如2345。(与open相同)。 If you use for example & separator for Thousands, you don't have the problem. 如果您使用分隔符作为千位分隔符,则不会有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM