1

I have a query table in a worksheet, which is imported from another excel file (using the Power Query Editor). The worksheet is the only one in the workbook.

In a VBA procedure inside This workbook module, I try to assign my querytable to a variable:

Private WithEvents QT As QueryTable
Set QT = Activesheet.QueryTables(1)

But I get the error “subscript out of range”. After some investigation, I tried to count the number of querytables in my worksheet:

MsgBox ActiveSheet.QueryTables.Count

and the result was 0 (zero). Therefore, the VBA can’t “find” my table. What am I doing wrong? I am new in the query tables world, so any help is very welcome.

1 Answer 1

2

When data is imported using the Power Query Editor, Excel loads the result into a table (a ListObject) rather than as a QueryTable. Instead you can use the following:

Private WithEvents QT As QueryTable

Sub InitializeAndManipulateQueryTable()

    Dim lo As ListObject
    
    Set lo = ActiveSheet.ListObjects(1)
    Set QT = lo.QueryTable
    
End Sub
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.