+ Reply to Thread
Results 1 to 8 of 8

Importing a Text File Using a Macro

Hybrid View

  1. #1
    Registered User
    Join Date
    01-07-2013
    Location
    Warwickshire, England
    MS-Off Ver
    Excel 2010
    Posts
    7

    Importing a Text File Using a Macro

    Hi,

    I am new to using excel macros and I am wondering how to go about this.

    I need import 4 text files from the same directory. However I need to be able to import the file path into a cell (for example A1), and the file name to be in the macro. I think I have almost done this but I keep getting a run time 1004 error.

    Below is what I have so far to import just 1 text file.

    Sub txt_import()
    '
    ' txt_import Macro
    '
    ' Keyboard Shortcut: Ctrl+t
    
    Dim FilePath As String
    FilePath = Range("A1").Value
    
        With ActiveSheet.QueryTables.Add(Connection:= _
            "TEXT;FilePath & Textfile1.txt", Destination:=Range("$A$17"))
            .name = "avg_fft_di_8"
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .TextFilePromptOnRefresh = False
            .TextFilePlatform = 850
            .TextFileStartRow = 1
            .TextFileParseType = xlDelimited
            .TextFileTextQualifier = xlTextQualifierDoubleQuote
            .TextFileConsecutiveDelimiter = True
            .TextFileTabDelimiter = True
            .TextFileSemicolonDelimiter = False
            .TextFileCommaDelimiter = False
            .TextFileSpaceDelimiter = True
            .TextFileColumnDataTypes = Array(1, 1)
            .TextFileTrailingMinusNumbers = True
            .Refresh BackgroundQuery:=False
    End With
    End Sub
    If someone could help me with this it would be most appreciated.

    Thanks.

    Moderator's note: Welcome to the forum James. When posting codes they should be enclosed with code tags. Select the code then click the "#" symbol to wrap it with code tags. I'll do it for you now. Thank you!
    Last edited by vlady; 01-07-2013 at 07:37 PM.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Importing a Text File Using a Macro

    Assuming FilePath has a valid path and 'TextFile.txt' is a valid filename try this.
    With ActiveSheet.QueryTables.Add(Connection:= _
     "TEXT;"& FilePath & "Textfile1.txt", Destination:=Range("$A$17"))
    If posting code please use code tags, see here.

  3. #3
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Importing a Text File Using a Macro

    .....-----------------------------
    If solved remember to mark Thread as solved

  4. #4
    Registered User
    Join Date
    01-07-2013
    Location
    Warwickshire, England
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Importing a Text File Using a Macro

    Thank you for your reply, however I still get "Run-time error 1004". Do you have any idea what this problem could be? I have checked the filepath and filename and they are both correct.

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Importing a Text File Using a Macro

    Whats the actual error message?

  6. #6
    Registered User
    Join Date
    01-07-2013
    Location
    Warwickshire, England
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Importing a Text File Using a Macro

    This is the error message I get.

    Run Time Error Message.jpg

  7. #7
    Forum Contributor PingPing's Avatar
    Join Date
    02-19-2010
    Location
    London, England
    MS-Off Ver
    2007
    Posts
    158

    Re: Importing a Text File Using a Macro

    Check that 'FilePath' has a trailing backslash eg. c:\temp\
    Are the text files, in fact, Tab Delimited - as you have told the QueryTable they are (".TextFileTabDelimiter = True")?

    FWIW, unless you're wedded to QueryTables I'd recommend tackling your problem with ADO instead.

  8. #8
    Registered User
    Join Date
    01-07-2013
    Location
    Warwickshire, England
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Importing a Text File Using a Macro

    It now works, thank you all for you help.

    James

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1