+ Reply to Thread
Results 1 to 2 of 2

Passing on Variable path to connection inside query table

Hybrid View

  1. #1
    Registered User
    Join Date
    11-18-2014
    Location
    Singapore
    MS-Off Ver
    2010
    Posts
    20

    Passing on Variable path to connection inside query table

    Hi,

    I wanted to write a macro, that imports a certain text file.

    I am using the following code, which prompts a certain error like object not defined

    MsgBox ("Choose adhoc File"):
    updatefilename = Application.GetOpenFilename
    'Set adhocBook = Workbooks.filepath
    Set mainbook = ActiveWorkbook
    
    Workbooks.Open (updatefilename)
    
    
    adhocpath = Application.ActiveWorkbook.FullName
    
    mainbook.Activate
    
    
    '
    Sheets("adhoc1").Select
    
        With ActiveSheet.QueryTables.Add(Connection:= _
            " & adhocpath & ", Destination _
            :=Range("$A$1"))
            .Name = "ADHOC"
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .TextFilePromptOnRefresh = False
            .TextFilePlatform = 65001
            .TextFileStartRow = 1
            .TextFileParseType = xlDelimited
            .TextFileTextQualifier = xlTextQualifierDoubleQuote
            .TextFileConsecutiveDelimiter = False
            .TextFileTabDelimiter = True
            .TextFileSemicolonDelimiter = False
            .TextFileCommaDelimiter = False
            .TextFileSpaceDelimiter = False
            .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
            .TextFileTrailingMinusNumbers = True
            .Refresh BackgroundQuery:=False
    i have italicized on which line the error is coming.

    Thank You,
    Bharat

  2. #2
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: Passing on Variable path to connection inside query table

        With Sheets("adhoc1").QueryTables.Add(Connection:=adhocpath, Destination:=Sheets("adhoc1").Range("$A$1"))
    You can drop Sheets("adhoc1").Select line
    If you are pleased with a member's answer then use the Star icon to rate it.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] I want to set ThisWorkbook.Path inside a variable in order to open a file.
    By grid in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-21-2013, 11:07 AM
  2. Replies: 1
    Last Post: 06-01-2011, 03:30 AM
  3. Automated Import of .csv File into Excel Using Variable as Connection Path?
    By dsfcom in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-05-2008, 10:50 AM
  4. Place a variable inside a directory path
    By Brad J. in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-24-2006, 11:38 AM
  5. [SOLVED] variable for path name in ODBC connection
    By MOI_Jim in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-13-2005, 10:06 AM

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