Results 1 to 3 of 3

Thread: [RESOLVED] Open CSV File - Please, Help!

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2009
    Posts
    3

    Resolved [RESOLVED] Open CSV File - Please, Help!

    Good Morning everyone,

    I'm having trouble importing CSV via macro in Excel 2007.
    I found several answers here but none of them solved my problem.

    When I open the file or try to import it, the formatting is lost. I found functions to the CSV format but the only one that worked I can not include a variable to read the path of the CSV

    This works for me:

    Code:
    With ActiveSheet.QueryTables.Add(Connection:= _
            "TEXT; [here goes the path with the file]"  _
            , Destination:=Range("$A$1"))
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .TextFilePromptOnRefresh = False
            .TextFilePlatform = 1252
            .TextFileStartRow = 1
            .TextFileParseType = xlDelimited
            .TextFileTextQualifier = xlTextQualifierDoubleQuote
            .TextFileConsecutiveDelimiter = False
            .TextFileTabDelimiter = True
            .TextFileSemicolonDelimiter = True
            .TextFileCommaDelimiter = True
            .TextFileSpaceDelimiter = False
            .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
            1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 _
            , 1, 1, 1, 1, 1, 1, 1, 1, 1)
            .TextFileTrailingMinusNumbers = True
            .Refresh BackgroundQuery:=False
        End With
    But, i can not put a variable after the code "TEXT;" I don't know how to do it.

    Someone help me please. I'm going crazy with it. This function format my CSV but I can not point the variable to open the file.

    Thank you all for help,

    Hugs,
    Caio

  2. #2

    Thread Starter
    New Member
    Join Date
    Dec 2009
    Posts
    3

    Re: Open CSV File - Please, Help!

    I'm sorry people. I resolved my problem

    I do this:

    Code:
    Arquivo = "TEXT;" + sLinha
    
    With ActiveSheet.QueryTables.Add(Connection:= _
            Arquivo _
            , Destination:=Range("$A$1"))
            .FieldNames = True
            .RowNumbers = False
    Tks everyone.

    Hugs,
    Caio

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [RESOLVED] Open CSV File - Please, Help!

    you can try this

    vb Code:
    1. mypath = "c:\temp\myfile.xls"
    2. With ActiveSheet.QueryTables.Add(Connection:= _
    3.         "TEXT; " & mypath  _
    4.         , Destination:=Range("$A$1"))
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width