Code for timed query refresh

joelnichols

Active Member
Joined
Apr 13, 2004
Messages
406
Office Version
  1. 2003 or older
Platform
  1. Windows
I was given this code for a timed query refresh but it give an error. Can anyone correct it?

This is in module1:

Sub QueryTableRefresh()
Dim qt As QueryTable
Application.OnTime Now + TimeValue("00:00:05"), "QueryTableRefresh"
For Each qt In Worksheet("Sheet1").QueryTables
qt.Refresh
Next qt

This is in "this workbook" under "workbook" and "open"

Private Sub Workbook_Open()


QueryTableRefresh
 
O.K. that was the prob. The sheet name was "query" but it said "sheet1" next to it so I assumed either/or would work (duh). 1 more question. If i have 5 sheets with queries what do I need to change so they all update?

Thanks for all your help
 
Upvote 0

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Probably easiest to do this:

Code:
Sub QueryTableRefresh()
Dim qt As QueryTable, ws As Worksheet

For Each ws In ThisWorkbook.Worksheets

    For Each qt In ws.QueryTables
    
    qt.Refresh
    
    Next qt

Next ws
End Sub

This will cycle thru all the workbooks sheets and refresh all the query tables therein (it doesn't matter if there are no querytables in any worksheet, it will simply skip to the next worksheet).

Regards

Richard
 
Upvote 0
I think i put a line in the wrong spot and it errors out. Now excel shuts down without even letting me see the code lol.

Any ideas how to get to the code now?
 
Upvote 0
Joel

Do you mean that if you open Excel up and go to the VBE you cannot click on the module containing the code without Excel closing down? You did copy that code into a standard module (probably called "Module1")?

Richard
 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,606
Members
449,089
Latest member
Motoracer88

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