How do you refresh SQL data in Excel with VBA?

nmay732

New Member
Joined
Jun 20, 2012
Messages
12
I have a correctly connected query table on a worksheet called "SQL Data" that I can refresh manually no problem.
However, when I run this VBA code I get the following error:

Dim ws As Worksheet
Set ws = Worksheets("SQL Data")

'Refresh the SQL Table
ws.QueryTables(1).Refresh

>>ERROR: Subscript out of range (On "ws.QueryTables(1).Refresh")

On further inspection using the Locals pane, I found the count of the object QueryTables for ws is 0

Why is the table not being added into QueryTables?
Is there another way to refresh the data with VBA?
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
maybe simplest is thisworkbook.refreshall

note refreshes pivot tables & query tables

otherwise loop through each wks and through each qt and refresh one at a time - using for each to loop the collections

hth
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,173
Members
451,543
Latest member
cesymcox

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