简体   繁体   中英

Opening a CSV file with semicolon delimiter

I have a simple macro that needs to open a .csv file with semicolon delimiter, however when I run my code all the data is in column one with ; in the strings but not delimited.

I've tried different optional settings for the workbooks.open method. Below I included my latest attempt. My regional settings are English (United Kingdom) with , as digit grouping seperator and . for decimal seperator.

My code so far:

Sub OpenCSV()

Dim FilePath as String
Dim LikeFile as String

FilePath = " C:\" ' Name of file path to fetch data from
LikeFile = Dir(FilePath & "*.csv")    ' Finding file 
        If LikeFile <> "" Then
            Workbooks.Open Filename:=FilePath & LikeFile, Format:=4, Delimiter:=";", ReadOnly:=True, Local:=True
        End if
End sub

My actual result is that it opens the file without delimiting.

Snippet of my .csv file:

Nav as per;Fund Id;Name of Fund;Type of Share;Currency;ISIN Code;Price (NAV);Total NAV;Outstanding;Tis;Tid;Share ccy; Share price; TNA share ccy;Exchange rate 20190321;XX9999;NamedFund;EUI;USD;XX123123;204.563000000;165061156.750000000;806896.369000000;;;EUR;180.070000000;145293919.06;1.136050000 20190321;XX9999;NamedFund;EUR;USD;XX123124;193.437000000;352547.160000000;1822.538000000;;;EUR;170.270000000;310327.15;1.136050000 20190321;XX9999;NamedFund;USI;USD;XX123125;104.942000000;37779152.540000000;360000.000000000;;;;;; 20190321;XX9999;NamedFund;USR;USD;XX123126;0.000000000;0.000000000;0.000000000;;;;;;

Sounds like you need to utilize .TextToColumns; it can specify to separate on semicolon, eg:

Columns(1).TextToColumns DataType:=xlDelimited, _
    TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
    Semicolon:=True, Comma:=False, Space:=False, Other:=False

Edit1:

Note that you WILL have to fix your data as it will show up as a text string, eg, perform a loop over your numbers and multiple by 1... i posted somethign similar yesterday regarding pivot tables displaying #Value when numbers existed in the source data:

With sws 'source worksheet
    lr = .Cells(.Rows.Count, 1).End(xlUp).Row
    For i = 2 To lr
        .Cells(i, 6).Value = Val(.Cells(i, 6).Value) * 1 'Uses column 6 (F)
    Next i
End With

Delimited import works better if you rename your .CSV into .TXT programmatically before importing it:

Private Sub CSVimportV2()
    Dim wb As Workbook
    Dim tmpFilename As String
    Dim myPath As String

    myPath = Application.DefaultFilePath & "\example.csv"

    ' delete .txt without questioning
    tmpFilename = Left(myPath, InStrRev(myPath, ".")) & "txt"
    If Dir(tmpFilename) <> "" Then Kill tmpFilename

    ' copy .csv as additional .txt
    FileCopy myPath, tmpFilename

    ' open .txt as new workbook
    Set wb = Workbooks.Open( _
        Filename:=tmpFilename, _
        UpdateLinks:=0, _
        ReadOnly:=True, _
        format:=4, _
        Password:="", _
        WriteResPassword:="", _
        IgnoreReadOnlyRecommended:=False, _
        Origin:=xlWindows, _
        Delimiter:=";", _
        Editable:=True, _
        Notify:=True, _
        Converter:=2, _
        AddToMru:=False, _
        Local:=True, _
        CorruptLoad:=xlNormalLoad)

    ' delete .txt
    If Dir(tmpFilename) <> "" Then Kill tmpFilename

    ' save it als xlsx
    wb.Sheets(1).UsedRange.EntireColumn.AutoFit
    wb.SaveAs Left(myPath, InStrRev(myPath, ".")) & "xlsx", xlOpenXMLWorkbook
End Sub

If your decimal separator is not recognized correctly, please adapt Local (can be True or False )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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