Trouble with Query Tables & 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'
Worksheets("SQL Data").QueryTables(1).Refresh

>>ERROR: Subscript out of range

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?
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Excel 2007 uses a new syntax for addressing query table objects - maybe that's all? Try the syntax shown here:
QueryTablesXL2007
 
Upvote 0
Thanks for replying!

I actually found that using ActiveWorkbook.RefreshAll did a clean job of refreshing my single table.
 
Upvote 0
Actually, I ran into an issue where I wanted to edit the command for a table, where I still needed to access the table data. Your link is really the better option. Here's the code I used to just view the command of my table if anyone has the same issue:

Code:
Dim lo As ListObject

For Each lo In MyWorksheet.ListObjects
    Debug.Print (lo.QueryTable.CommandText)
Next

I should have specified in the original post that I'm using Excel 2010.

Thanks again!
 
Upvote 0

Forum statistics

Threads
1,203,082
Messages
6,053,418
Members
444,662
Latest member
AaronPMH

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