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
Bookmarks