Save as .csv correct decimal separator

时间:2015-11-12 12:07:29

标签: excel vba excel-vba csv decimal

I'm writing a "master" Excel document that creates new workbooks and saves them as ".csv" by using Visual Basic for Applications. The created workbooks contain measured data from two sources. One source already delivers the measure date in my regional settings (Holland, in which the comma , sign is commonly to used as decimal separator). The other source however delivers data with a dot . as separator (American settings). I'd like to replace all dot signs for comma signs.

Is this possible with the Replace function?

(I guess this solution would only fail when applying it on values greater than 1.000,00 (or 1,000.00 for US settings). Which would be acceptable but far from perfect.

Attempts so far

  • Application.DecimalSeperator = ","
  • Add, local = true after the line where the workbook is saved

In Addition

Excel thinks the values from the text document have 1000 separators 31.435. When i turn of the 1000-separator the value is 31435. I need it to be 31,435 (decimal Separated by comma , sign)

Do I need to adapt the cell format?
If so, then... How?
Any other options to solve this?

Anwser/Sollution(that works best for me)

Sheets(SheetNaamCNC).Range("B1").Select
Do While ActiveCell.Value <> ""
    If InStr(ActiveCell.Value, "0,") = 0 Then
        ActiveCell.Value = ActiveCell.Value / 1000
    End If
    ActiveCell.Offset(1, 0).Select
Loop

4 个答案:

答案 0 :(得分:2)

使用数据标签&gt;将文本文件读入Excel工作表时获取外部数据&gt; 从文本,您可以通过单击文本文件导入向导的第3步中的Advanced按钮来指定小数点和千位分隔符。

enter image description here

如果您在导入其中一个数据文件时录制宏,则会得到如下内容:

With ActiveSheet.QueryTables.Add(Connection:= _
    "TEXT;myfilename.csv" _
    , Destination:=Range("$A$1"))
    .Name = "myfilename"
    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .TextFilePromptOnRefresh = False
    .TextFilePlatform = 850
    .TextFileStartRow = 1
    .TextFileParseType = xlDelimited
    .TextFileTextQualifier = xlTextQualifierDoubleQuote
    .TextFileConsecutiveDelimiter = False
    .TextFileTabDelimiter = False
    .TextFileSemicolonDelimiter = False
    .TextFileCommaDelimiter = True
    .TextFileSpaceDelimiter = False
    .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1)
    .TextFileDecimalSeparator = ","
    .TextFileThousandsSeparator = " "
    .Refresh BackgroundQuery:=False
End With

然后您可以使用录制的代码开发自己的宏,以正确的格式导入未来的数据文件 - 特别参见TextFileDecimalSeparatorTextFileThousandsSeparator属性。

答案 1 :(得分:1)

您可以使用下面的功能

Sub sub1()
    Application.DecimalSeparator = "."
    Application.ThousandsSeparator = ","
    Application.UseSystemSeparators = False
    Plan1.Columns(1).NumberFormat = "#,##0.00"
End Sub

答案 2 :(得分:1)

如果它以文字形式阅读,您可以尝试

=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(sheet1!A2,",","|"),".",","),"|",".")

如果我这样做的话,我会在我想要重新格式化的每一列的新工作表的同一列中输入此公式,并使用

复制任何其他列
=sheet1!B2 etc.

这取决于你有多少列,这是否是一个可行的解决方案。

答案 3 :(得分:0)

已经设定,作为分隔符。这适用于像0,234这样的值,但像31,345这样的值将失败。它们显示为31.345(在本例中为。千位分隔符)。 可能的溶解逐个循环通过细胞。如果第一个charachter不等于“0”,则将ActiveCell.Value除以1000。 绕道而行......但我想这对我有用。

添加以下内容,

Sheets("YourSheetName").Range("TopOfRangeYouLikeToEdit").Select
Do While ActiveCell.Value <> ""
    If Left(ActiveCell.Value, 1) <> "0" Then
        ActiveCell.Value = ActiveCell.Value / 1000
    End if
    ActiveCell.Offset(1,0).Select
Loop

- Cornelis编辑: 而不是Left(ActiveCell.Value,1)<> "0"

If ActiveCell.Value < 0 Then  

会更好

相关问题