Does querytables.add -require- activesheet

effort1584

New Member
Joined
Apr 6, 2010
Messages
2
My workbook will have between 20 and 200 sheets (situation dependent) with querytables added to each sheet. With that quantity I am hoping to not have to make each sheet active to get a query added. However, my testing so far shows it works every time the current query is referring to the active sheet and fails if it is not.

I have tried a couple of ways of referring to the sheet where I need to add the query.

Sheets(3).querytables.add
Sheets("shtName").querytables.add
dim a worksheet and set it using both examples above

Of course activesheet.querytables.add always works.

I am using Excel 2010, however I tested in Excel 2003 with the same result.

Is there another way to refer to the sheet to avoid using activesheet?
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Welcome to the Board!

I was able to add a query to the non-active sheet in Excel 2003 using the following format. Note that the worksheet name in the 'Destination' portion of the code must match the worksheet name in the 'With' portion of the code:
Rich (BB code):
    Sheets("Sheet2").Select 'Ensuring active sheet is not the destination
    With Worksheets("Sheet3").QueryTables.Add(Connection:=Array(Array( _
        "ODBC;DBQ=J:\AIM Data\B.XLS;DefaultDir=J:\AIM Data;Driver={Driver do Microsoft Excel(*.xls)};DriverId=790;FIL=excel 8.0;MaxBufferSize" _
        ), Array( _
        "=2048;MaxScanRows=8;PageTimeout=5;ReadOnly=1;SafeTransactions=0;Threads=3;UserCommitSync=Yes;" _
        )), Destination:=Sheets("Sheet3").Range("A1"))
        .CommandText = Array( _
        "SELECT `Sheet1$`.A, `Sheet1$`.B, `Sheet1$`.C, `Sheet1$`.`'D'`, `Sheet1$`.E, `Sheet1$`.F, `Sheet1$`.G, `Sheet1$`.H, `Sheet1$`." _
        , _
        "I, `Sheet1$`.J, `Sheet1$`.K" & Chr(13) & "" & Chr(10) & "FROM `J:\L\B`.`Sheet1$` `Sheet1$`" _
        )
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,958
Messages
6,122,475
Members
449,087
Latest member
RExcelSearch

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