Results 1 to 7 of 7

Excel Macro referencing a variable file name to import

Threaded View

  1. #1
    Registered User
    Join Date
    10-30-2008
    Location
    USA
    Posts
    2

    Excel Macro referencing a variable file name to import

    Hi All

    I have this Macro for Excel which imports 1 file C:\datafiles\user1-data-1.txt into cell E52:


    Sub Macro2()
    '
    ' Macro2 Macro
    ' Macro recorded 10/29/2008 by jouell
    '
    
    '
        With ActiveSheet.QueryTables.Add(Connection:= _
            "TEXT;C:\datafiles\user1-data-1.txt", Destination:=Range("E52"))
            .Name = "user1-data-1"
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .TextFilePromptOnRefresh = False
            .TextFilePlatform = 437
            .TextFileStartRow = 1
            .TextFileParseType = xlDelimited
            .TextFileTextQualifier = xlTextQualifierDoubleQuote
            .TextFileConsecutiveDelimiter = False
            .TextFileTabDelimiter = True
            .TextFileSemicolonDelimiter = False
            .TextFileCommaDelimiter = False
            .TextFileSpaceDelimiter = False
            .TextFileColumnDataTypes = Array(1)
            .TextFileTrailingMinusNumbers = True
            .Refresh BackgroundQuery:=False
        End With
        Application.Goto Reference:="Macro2"
    End Sub
    I'd like to be able to set the file name to import based on the name of excel file that I'm importing data to.


    I.E

    This piece:

       With ActiveSheet.QueryTables.Add(Connection:= _
            "TEXT;C:\datafiles\user1-data-1.txt", Destination:=Range("E52"))
    Would become:

       With ActiveSheet.QueryTables.Add(Connection:= _
            "TEXT;C:\datafiles\name_of_excel_file-data-1.txt", Destination:=Range("E52"))
    I.E if the Main Excel file name is CoolDoc.xls, the macro imports CoolDoc-data1.txt.


    Thanks!
    -jouell
    Last edited by rylo; 12-08-2008 at 10:24 PM. Reason: code tags and SOLVED

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