How to import a csv file using @ as delimiter with VBA

旧巷老猫 提交于 2021-02-07 07:15:50

问题


I am trying to load data from a csv file in Excel with VBA using ADODB.
I have a function to return a Connection object.

Private Function OpenConnection(dataSource As String) As ADODB.Connection
    Set OpenConnection = CreateObject("ADODB.Connection")

    With OpenConnection
        .ConnectionTimeout = 5
        .ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dataSource & ";" & _
            "Extended Properties=""Text;HDR=YES;FMT=Delimited(,)"";Persist Security Info=False"

        Debug.Print "trying to connect: " & .ConnectionString
        .Open
    End With
End Function

And then I just print the data.

Public Sub Test_Import()
    Dim conn As ADODB.connection, records As ADODB.Recordset

    Set connection = OpenConnection(foldername)
    Set records = connection.Execute("Select * from data.txt")

    Debug.Print records.Fields(0)
End Sub

If I use commas it works fine but in the end I will have to use a file that is separated by '@' symbols and which I cannot convert to using ',' because of missing write permissions.
Copying and changing the file somewhere else is unfortunately also not an option.

Now I changed FMT=Delimited(,) to FMT=Delimited(@) in the function OpenConnection and instead of returning the first column value a1, the full line a1@b1@c1 is returned.

Is '@' not supported as a delimiting string? Or did I miss something?


回答1:


Thanks to @SiddharthRout for the solution and the link to a thread at windows dev center.

The problem was that in the connection string one cannot set the delimiting string but only specify that the file is delimited using a specific character. FMT=Delimited(@) is treated just the same as FMT=Delimited or FMT=Delimited(,).

I had to create a file schema.ini in the folder that contains the csv-file (data.txt) where I had to enter:

[data.txt]
Format = Delimited(@)

The schema.ini file will be parsed and the delimiter is read correctly and everything works (as long as I don't change any file names).

I also found another source at msdn that explains how to set the delimiting string (using either the registry or the mentioned schema.ini) and also includes Tabstops and fixed length separated files.




回答2:


@marcw - You are awesome. To help You guys more - You don't have to create manually this file. If You are using Filedialog it can looks like this:

Set fldr = Application.FileDialog(msoFileDialogOpen)

With fldr
    .Title = "Select a Folder"
    .AllowMultiSelect = False       ' Select only 1 file
    .InitialFileName = strPath      ' File location
    If .Show <> -1 Then Exit Sub    ' Quit when user cancels
    myFolder = .SelectedItems(1)
End With

'you can find below function on stackoverflow
strPath = Left(myFolder, Len(myFolder) - Len(GetFilenameFromPath(myFolder)) - 1) 'file path ended with \
nejm = GetFilenameFromPath(myFolder) 'file name

'this is the place which is creating that "ini" file
Open strPath & "\" & "schema.ini" For Output As #1
Print #1, "[" & nejm & "]" & vbNewLine & "Format = Delimited(;)"
Close #1

After all processes You can just add:

Kill strPath & "\" & "schema.ini"


来源:https://stackoverflow.com/questions/21525951/how-to-import-a-csv-file-using-as-delimiter-with-vba

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!