Import semicolon separated CSV file using VBA

后端 未结 1 1383
遥遥无期
遥遥无期 2020-12-07 03:06

I have a problem with opening .csv files with Excel by VBA code. I have data organised like:

Number;Name;Price1;Price2;City 
1234;\"John Smith\";\"1,75 EUR\"         


        
相关标签:
1条回答
  • 2020-12-07 03:13

    I hope you are using something newer than Excel 2000. This is what I experience with Excel 2016 on a machine with German format settings: Apparently the Workbooks.Open options for delimiters (Format and Delimiter) are only applied when you open a .txt file. If Local is set to False (the default value), the file will be opened with the VBA language settings, using a comma as delimiter. Setting Local:=True will prevent this so

    Workbooks.Open FileName:=strPath & "thisFile.csv", Local:=True
    

    should work for you. If you rename your file to .txt, you can use the Format and Delimiter options:

    Workbooks.Open FileName:=strPath & "thisFile.txt", Format:=4 'Format = 4 is semicolon delimited
    Workbooks.Open FileName:=strPath & "thisFile.txt", Format:=6, Delimiter:=";" 'Format = 6 is custom delimited
    

    See MSDN for more details
    However this will mess up your decimal numbers, see my edit.


    Edit: I misread the documentation. The Format and Delimiter options are actually only applied when using a .txt file and not .csv (even the .OpenText method behaves that way).

    If you want to make sure it opens on a machine with different format settings the only solution I have right now is to rename it to .txt and use

    Workbooks.OpenText Filename:=FileName:=strPath & "thisFile.txt", DataType:=xlDelimited, Semicolon:=True, DecimalSeparator:=",", ThousandsSeparator:="."
    
    0 讨论(0)
提交回复
热议问题