IMPORTRANGE Syntax for Using Query on Multiple Tabs in Google Sheets

google sheetsgoogle-sheets-queryimportrange

I am using IMPORTRANGE within a query to extract data from sheet 1 to sheet 2. The following code is in a cell in tab A of sheet 2, and works as intended:

=query(importrange("URL-A","E:J"), "select * where Col6 = TRUE AND Col4 IS NOT NULL")

That is, it extracts the correct data from sheet 1, tab A. Note that URL-A is specific to tab A.

However, when I attempt to extract data from tab B:

=query(importrange("URL-B","C:J"), "select * where Col2 IS NOT NULL AND Col8 = TRUE AND Col6 IS NOT NULL")

where URL-B is specific to tab B, the data is still being extracted from tab A.

In other words, IMPORTRANGE seems to be extracting data from the first tab, despite specifying URLs that are specific to other tabs.

Is there additional or different IMPORTRANGE syntax required to refer to different tabs?

Best Answer

Instead of using "E:J" and "C:J" use "tab a!E:J" and "tab b!C:J" where tab a and tab b are the tab names.

NOTE: To avoid confusion, instead of using URL use the spreadsheet key, because this makes clear that the first parameter refers to the spreadsheet only.