Hey guys,
I'm having an odd, intermittent error when running a macro in Excel which runs a query to a separate excel spreadsheet.
It's a file that we have on a network location for multiple people to use.
Sometimes when I run the macro, the search doesn't function properly and instead opens up the query source file.
It usually corrects itself after a few minutes.
The script is here:
Sub SEARCH_AND_FILTER()
'
' SEARCH_AND_FILTER Macro
'
'
Application.ScreenUpdating = False
On Error Resume Next
ActiveSheet.ListObjects("Table_Query_from_Excel_Files_1"). _
Range.AutoFilter Field:=1
ActiveSheet.ListObjects("Table_Query_from_Excel_Files_1").Range.AutoFilter _
Field:=2
ActiveSheet.ListObjects("Table_Query_from_Excel_Files_1").Range.AutoFilter _
Field:=3
ActiveSheet.ListObjects("Table_Query_from_Excel_Files_1").Range.AutoFilter _
Field:=4
Rows("9:100000").Select
Selection.Delete Shift:=xlUp
Range("D11").Select
Columns("C:C").EntireColumn.AutoFit
Range("C9").Select
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array(Array( _
"ODBC;DSN=Excel Files;DBQ=\OURDIRECTORYHERE\OURFILE.xls;DefaultDir=\OURD" _
), Array( _
"IRECTORYHERE\OURFILE;DriverId=1046;MaxBufferSize=2048;PageTimeout=5;" _
)), Destination:=Range("$C$9")).QueryTable
.CommandText = Array( _
"SELECT `Postcodes$`.Postcode, `ArrayFormat$`.`Business Partner`, `ArrayFormat$`.Qualification, `ArrayFormat$`.P, `Agents$`.Phone, `Agents$`.`E-Mail`" & Chr(13) & "" & Chr(10) & "FROM `Agents$` `Agents$`, `ArrayFormat$` `ArrayFor" _
, _
"mat$`, `Postcodes$` `Postcodes$`" & Chr(13) & "" & Chr(10) & "WHERE `Agents$`.Name = `ArrayFormat$`.`Business Partner` AND `ArrayFormat$`.`Postal Code Area` = `Postcodes$`.`Postal Code Area`" _
)
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "Table_Query_from_Excel_Files_1"
.Refresh BackgroundQuery:=False
End With
Columns("F:F").EntireColumn.AutoFit
Range("C4").Select
ActiveSheet.ListObjects("Table_Query_from_Excel_Files_1").Range.AutoFilter _
Field:=1, Criteria1:=Range("C4").Value, Operator:=xlAnd
ActiveWindow.SmallScroll Down:=-15
Range("C4").Select
ActiveSheet.ListObjects("Table_Query_from_Excel_Files_1").Range.AutoFilter _
Field:=2, Criteria1:=Range("E4").Value, Operator:=xlAnd
ActiveSheet.ListObjects("Table_Query_from_Excel_Files_1").Range.AutoFilter _
Field:=3, Criteria1:=Range("D4").Value, Operator:=xlAnd
ActiveSheet.ListObjects("Table_Query_from_Excel_Files_1").Range.AutoFilter _
Field:=4, Criteria1:=Range("F4").Value, Operator:=xlAnd
If Range("C2") = "True" Then ActiveSheet.ListObjects("Table_Query_from_Excel_Files_1"). _
Range.AutoFilter Field:=1
If Range("E2") = "True" Then ActiveSheet.ListObjects("Table_Query_from_Excel_Files_1").Range.AutoFilter _
Field:=2
If Range("D2") = "True" Then ActiveSheet.ListObjects("Table_Query_from_Excel_Files_1").Range.AutoFilter _
Field:=3
If Range("F2") = "True" Then ActiveSheet.ListObjects("Table_Query_from_Excel_Files_1").Range.AutoFilter _
Field:=4
Application.ScreenUpdating = True
End Sub
Help with this would be SERIOUSLY appreciated!
Thanks very much!
Rosie
I'm having an odd, intermittent error when running a macro in Excel which runs a query to a separate excel spreadsheet.
It's a file that we have on a network location for multiple people to use.
Sometimes when I run the macro, the search doesn't function properly and instead opens up the query source file.
It usually corrects itself after a few minutes.
The script is here:
Sub SEARCH_AND_FILTER()
'
' SEARCH_AND_FILTER Macro
'
'
Application.ScreenUpdating = False
On Error Resume Next
ActiveSheet.ListObjects("Table_Query_from_Excel_Files_1"). _
Range.AutoFilter Field:=1
ActiveSheet.ListObjects("Table_Query_from_Excel_Files_1").Range.AutoFilter _
Field:=2
ActiveSheet.ListObjects("Table_Query_from_Excel_Files_1").Range.AutoFilter _
Field:=3
ActiveSheet.ListObjects("Table_Query_from_Excel_Files_1").Range.AutoFilter _
Field:=4
Rows("9:100000").Select
Selection.Delete Shift:=xlUp
Range("D11").Select
Columns("C:C").EntireColumn.AutoFit
Range("C9").Select
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array(Array( _
"ODBC;DSN=Excel Files;DBQ=\OURDIRECTORYHERE\OURFILE.xls;DefaultDir=\OURD" _
), Array( _
"IRECTORYHERE\OURFILE;DriverId=1046;MaxBufferSize=2048;PageTimeout=5;" _
)), Destination:=Range("$C$9")).QueryTable
.CommandText = Array( _
"SELECT `Postcodes$`.Postcode, `ArrayFormat$`.`Business Partner`, `ArrayFormat$`.Qualification, `ArrayFormat$`.P, `Agents$`.Phone, `Agents$`.`E-Mail`" & Chr(13) & "" & Chr(10) & "FROM `Agents$` `Agents$`, `ArrayFormat$` `ArrayFor" _
, _
"mat$`, `Postcodes$` `Postcodes$`" & Chr(13) & "" & Chr(10) & "WHERE `Agents$`.Name = `ArrayFormat$`.`Business Partner` AND `ArrayFormat$`.`Postal Code Area` = `Postcodes$`.`Postal Code Area`" _
)
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "Table_Query_from_Excel_Files_1"
.Refresh BackgroundQuery:=False
End With
Columns("F:F").EntireColumn.AutoFit
Range("C4").Select
ActiveSheet.ListObjects("Table_Query_from_Excel_Files_1").Range.AutoFilter _
Field:=1, Criteria1:=Range("C4").Value, Operator:=xlAnd
ActiveWindow.SmallScroll Down:=-15
Range("C4").Select
ActiveSheet.ListObjects("Table_Query_from_Excel_Files_1").Range.AutoFilter _
Field:=2, Criteria1:=Range("E4").Value, Operator:=xlAnd
ActiveSheet.ListObjects("Table_Query_from_Excel_Files_1").Range.AutoFilter _
Field:=3, Criteria1:=Range("D4").Value, Operator:=xlAnd
ActiveSheet.ListObjects("Table_Query_from_Excel_Files_1").Range.AutoFilter _
Field:=4, Criteria1:=Range("F4").Value, Operator:=xlAnd
If Range("C2") = "True" Then ActiveSheet.ListObjects("Table_Query_from_Excel_Files_1"). _
Range.AutoFilter Field:=1
If Range("E2") = "True" Then ActiveSheet.ListObjects("Table_Query_from_Excel_Files_1").Range.AutoFilter _
Field:=2
If Range("D2") = "True" Then ActiveSheet.ListObjects("Table_Query_from_Excel_Files_1").Range.AutoFilter _
Field:=3
If Range("F2") = "True" Then ActiveSheet.ListObjects("Table_Query_from_Excel_Files_1").Range.AutoFilter _
Field:=4
Application.ScreenUpdating = True
End Sub
Help with this would be SERIOUSLY appreciated!
Thanks very much!
Rosie