Results 1 to 3 of 3

Creating a query in Excel using VBA giving an error.

Threaded View

  1. #1
    Registered User
    Join Date
    11-10-2011
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    7

    Creating a query in Excel using VBA giving an error.

    I am creating a query in Excel 2007 using below codes giving an error "Run-time error '1004': A table cannot overlap a range that contains a PivotTable report, query results, protected cells or another table."

    I want to replace the current data based on the value received from the input box.


    Sub Macro1()
    '
    ' Macro1 Macro
    '
    
    '
    Dim FileName As String
    Dim FilePath As String
    Dim Location As String
    
    FileName = Application.ActiveWorkbook.FullName
    FilePath = Application.ActiveWorkbook.Path
    Location = InputBox("Location: ")
    
        With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array(Array( _
            "ODBC;DSN=Excel Files;DBQ=" & FileName & ";DefaultDir=" & FilePath & ";DriverId=1046;Ma" _
            ), Array("xBufferSize=2048;PageTimeout=5;")), Destination:=Range("'Sheet3'!$A$1")). _
            QueryTable
            .CommandText = Array( _
            "SELECT `Lists$`.Location, `Lists$`.`Customer Name`" & Chr(13) & "" & Chr(10) & "FROM `Lists$` `Lists$`" & Chr(13) & "" & Chr(10) & "WHERE (`Lists$`.Location=" & Location & ")" & Chr(13) & "" & Chr(10) & "ORDER BY `Lists$`.`Customer Name`" _
            )
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .BackgroundQuery = True
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .PreserveColumnInfo = True
            .ListObject.DisplayName = "Customer_List"
            .Refresh BackgroundQuery:=False '                         THIS LINE IS ALSO GIVING AN ERROR "Application-defined or object-defined error.
        End With
    End Sub
    Last edited by sanjay.k.sahrma102; 11-22-2014 at 03:24 AM. Reason: Code tags were missing.

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 6
    Last Post: 08-16-2013, 08:46 AM
  2. [SOLVED] Range error in code, runs alone but not inside my full program, giving runtime error 1004
    By charizzardd in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-23-2012, 03:34 PM
  3. Creating Simple Excel Search Query
    By stevedes7 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-26-2009, 09:12 AM
  4. Creating a Query in Excel based on an Access Qry...
    By Glenda in forum Excel General
    Replies: 1
    Last Post: 03-23-2006, 03:54 PM
  5. Excel help giving runtime error
    By Hari Prasadh in forum Excel General
    Replies: 2
    Last Post: 01-24-2005, 05: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