Opening CSV in Excel using VBA causes data to appear in two columns instead of one

后端 未结 5 1510
闹比i
闹比i 2021-01-20 12:17

I created VBA code in Excel 2007/2010 to import data from a CSV file. Unfortunately, when I open the file programmatically, the data is split into two columns (A and B) for

相关标签:
5条回答
  • 2021-01-20 12:18

    I still suspect it's because the extension is CSV. What happens if you rename the file as a .txt?

    0 讨论(0)
  • 2021-01-20 12:28

    Add Local:=True as argument in Workbooks.Open
    Hope this might help!

    0 讨论(0)
  • 2021-01-20 12:32

    In order to import data with a separator that is not a comma, you should set the Format attribute to 6 in order to be able to define your delimiter, as described here. It should also work if you directly set Format to 4

    0 讨论(0)
  • 2021-01-20 12:37

    I know two possible workarounds:

    1) Change the extension from .csv to for example .xxx and open it like this:

    Workbooks.OpenText fileName:="file.xxx", _
    Origin:=xlMSDOS, StartRow:=1, DataType:=xlDelimited, TextQualifier:=1, _
    ConsecutiveDelimiter:=False, Tab:=False, Semicolon:=True, _
    Comma:=False, Space:=False, Other:=False, OtherChar:="", _
    TrailingMinusNumbers:=True, Local:=True
    

    If you use .csv or .xls, then the excel overrides the settings by it's default values from the OS.

    2) In Windows 10, change your locale setting from English - United States to English - United Kingdom. It's strange that it helps, it doesn't matter what the delimiter setting in advanced date/time is. In Windows 7 I think the delimiter setting worked.

    0 讨论(0)
  • 2021-01-20 12:38

    I think when you do it manually Excel is reading the delimiter as ";" and not just ;.

    Try this:

    Workbooks.Open Filename:=wkbCsvImport, Format:=xlDelimited, Delimiter:=""";"""
    

    EDIT:

    the only way I can get this to work is by changing the file extension from csv to txt and then run this code:

    Workbooks.OpenText Filename:=wkbCsvImport, _
                        DataType:=xlDelimited, semicolon:=True
    
    0 讨论(0)
提交回复
热议问题