Excel VBA refer to QueryTable objects by name

calendar_today Asked Aug 5, 2013
thumb_up 5 upvotes
history Updated April 14, 2026

Direct Answer

According to this MSDN link for ListObject there isn't any collection of QueryTables being a property of ListObjects. Correct code is: Set QT =…. This is an advisory response with reference links, ranked #169th of 303 by community upvote score, from 2013.


The Problem (Q-score 10, ranked #169th of 303 in the Excel VBA archive)

The scenario as originally posted in 2013

I am developing a MS Excel 2013 tool with VBA which involves the use of QueryTables. One inconvenience I am experiencing is accessing existing QueryTables within an Excel worksheet. Currently, the only method I can find to access a query table is by integer indexing. I came up with the following code for a quick proof of concept

Sub RefreshDataQuery()

Dim querySheet As Worksheet
Dim interface As Worksheet

Set querySheet = Worksheets("QTable")
Set interface = Worksheets("Interface")

Dim sh As Worksheet
Dim QT As QueryTable

Dim startTime As Double
Dim endTime As Double

Set QT = querySheet.ListObjects.item(1).QueryTable

startTime = Timer
QT.Refresh
endTime = Timer - startTime

interface.Cells(1, 1).Value = "Elapsed time to run query"
interface.Cells(1, 2).Value = endTime
interface.Cells(1, 3).Value = "Seconds"

End Sub

This works but I really don’t want to do it this way. The end product tool will have up to five different QueryTables. What I want is to refer to a QueryTable by its name.

What would be nice is if I could translate the code below

Set QT = querySheet.ListObjects.item(1).QueryTable

To something along the lines

Set QT = querySheet.ListObjects.items.QueryTable("My Query Table")

Any suggestions would be much appreciated.

Why this Range / Worksheet targeting trips people up

The question centers on reaching a specific cell, range, or workbook object. In Excel VBA, this is the #1 source of failures after activation events: every property (.Value, .Formula, .Address) behaves differently depending on whether the parent Workbook is explicit or implicit.


The Verified Solution — niche answer (below median) (+5)

Advisory answer — community consensus with reference links

Note: the verified answer below is a reference / advisory response rather than a copy-ready snippet.

According to this MSDN link for ListObject there isn’t any collection of QueryTables being a property of ListObjects. Correct code is:

Set QT = querySheet.ListObjects.items(1).QueryTable

What you possibly need is to refer to appropriate ListObject item like (just example code):

Dim LS as ListObject
Set LS = querySheet.ListObjects("My LO 1")
Set QT = LS.QueryTable

The other alternative is to refer to QT through WorkSheet property in this way:

Set QT = Worksheet("QTable").QueryTables("My Query Table")


When to Use It — classic (2013–2016)

Ranked #169th in its category — specialized fit

This pattern sits in the 99% tail relative to the top answer. Reach for it when your scenario closely matches the question title; otherwise browse the Excel VBA archive for a higher-consensus alternative.

What changed between 2013 and 2026

The answer is 13 years old. The Excel VBA object model has been stable across Office 2013, 2016, 2019, 2021, 365, and 2024/2026 LTSC, so the pattern still compiles. Changes that might affect you: 64-bit API declarations (use PtrSafe), blocked macros in downloaded files (Mark-of-the-Web), and the shift toward Office Scripts for web-first workflows.

help
Frequently Asked Questions

This is a below-median answer — when does it still fit?
expand_more

Answer score +5 vs the Excel VBA archive median ~4; this entry is niche. The score plus 10 supporting upvotes on the question itself (+10) means the asker and 4 subsequent voters all validated the approach.

This answer links out — what are the reference links worth following?
expand_more

Read the first external link for the canonical reference, then search this archive for a top-10 entry in the same category — advisory answers are best paired with a ranked code snippet to close the loop.

Published around 2013 — what’s changed since?
expand_more

Published 2013, which is 13 year(s) before today’s Office 2026 build. The Excel VBA object model has had no breaking changes in that window. Three things to re-test: (1) blocked macros on downloaded files (Mark-of-the-Web), (2) 64-bit API declarations (PtrSafe, LongPtr), (3) any shift toward Office Scripts for web scenarios.

Which Excel VBA pattern ranks just above this one at #168?
expand_more

The pattern one rank above is “Insert Columns Dynamically vba”. If your use case overlaps, compare both before committing.

Data source: Community-verified Q&A snapshot. Q-score 10, Answer-score 5, original post 2013, ranked #169th of 303 in the Excel VBA archive. Last regenerated April 14, 2026.