Share via

Loading CSV file programmatically into Excel

Anonymous
2010-09-16T09:54:39+00:00

Hello,

I have the need inside a macro to load a CSV file. All my attempts (Workbooks.Open or OpenText) resulted in the CSV file itself loaded in a single column instead of having all CSV fields loaded into separate columnus.

Do I have to parse myself each line and fill the cells ? Excel can load the CSV file when I simply open it manually, so I guess it can be done programmatically.

Any help appreciated.


-- Francois Piette

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2010-09-16T11:18:02+00:00

Try the below

Sub Macro1()

Dim wb As Workbook, strFile As String

strFile = "c:\1.txt"

Set wb = Workbooks.Add

With wb.Sheets(1).QueryTables.Add(Connection:="TEXT;" & strFile, _

Destination:=wb.Sheets(1).Range("A1"))

    .TextFileParseType = xlDelimited

    .TextFileOtherDelimiter = ";"

    .Refresh

End With

End Sub

Was this answer helpful?

2 people found this answer helpful.
0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Anonymous
    2010-09-16T11:32:32+00:00

    This works fine exactly as I wanted.

    Thanks a lot.


    -- Francois Piette

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2010-09-16T11:09:57+00:00

    This is something I already tested. But I have done it again to be sure. I replaced "Comma:=True" by "Semicolon:=True" since my file has semicolon as separator.

    It doesn't work. As I said before, Everything is loaded in the first column.

    Thanks anyway.


    -- Francois Piette

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2010-09-16T10:04:38+00:00

    Try the below.

    Workbooks.OpenText Filename:="C:\test.csv", StartRow:=1, _

    DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, Comma:=True

    Was this answer helpful?

    0 comments No comments