Random Glitch when Excel is running a VBA script with MS Query

RosieAU

New Member
Joined
Jun 26, 2015
Messages
3
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! :biggrin:

Rosie
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.

Forum statistics

Threads
1,215,201
Messages
6,123,617
Members
449,109
Latest member
Sebas8956

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top