FormulaCraft

How to combine QUERY with IMPORTRANGE in Excel and Google Sheets

Topic:QUERY (Google Sheets SQL)

Heads up: Excel and Google Sheets do this differently.

Excel
=IFERROR(VLOOKUP(A2,'[Source.xlsx]Sheet1'!$A:$D,2,0),"Not found")
Google Sheets
=QUERY(IMPORTRANGE("https://docs.google.com/spreadsheets/d/SPREADSHEET_ID/","Sheet1!A1:D"),"SELECT Col1, Col2, SUM(Col4) WHERE Col3 = 'Active' GROUP BY Col1, Col2 LABEL SUM(Col4) 'Total'",1)

Verified example

Computed by a real spreadsheet engine on the sample data below.

ClientRegionStatusRevenue
Acme CorpNorthActive12000
Beta LLCSouthInactive4500
Gamma IncNorthActive8750
Delta CoEastActive6300

=IFERROR(VLOOKUP(A2,'[Source.xlsx]Sheet1'!$A:$D,2,0),"Not found")Not found

Try it with your data

Edit the grid or formula, then run it through a real spreadsheet engine — no signup.

Sample data — click any cell to edit

Runs server-side · free · no signup

Step by step

  1. 1Obtain the source spreadsheet URL — it looks like https://docs.google.com/spreadsheets/d/SPREADSHEET_ID/.
  2. 2First, grant access by typing =IMPORTRANGE("URL","Sheet1!A1:D") in an empty cell and clicking 'Allow access' in the prompt that appears. You only need to do this once per source file.
  3. 3Once access is granted, wrap the IMPORTRANGE in QUERY: =QUERY(IMPORTRANGE("URL","Sheet1!A1:D"),"SELECT Col1, Col2, SUM(Col4) WHERE Col3 = 'Active' GROUP BY Col1, Col2 LABEL SUM(Col4) 'Total'",1).
  4. 4Note that when data comes from IMPORTRANGE the columns are named Col1, Col2, Col3, … (not A, B, C) inside the QUERY string.
  5. 5Set the headers argument (the last parameter of QUERY) to 1 if the imported range has a header row.

Tips

Need it for your exact data?

Describe your columns in plain English and get the precise formula for your sheet, with the right Excel or Sheets syntax.

Frequently asked

Does this work in Excel?

No. IMPORTRANGE and QUERY are both Google Sheets-only. In Excel, use Power Query (Data → Get Data → From Workbook) to connect to another .xlsx file and apply filters and aggregations there.

Why do I get a REF error after the formula works initially?

A #REF! error from IMPORTRANGE usually means the source file has been moved, renamed, or the sharing permission was revoked. Check that the URL is still valid and the source file is still shared with the account running the formula.

Can I IMPORTRANGE from multiple sheets in one QUERY?

Not directly in a single QUERY call. Use curly-brace array notation to stack ranges: ={IMPORTRANGE("URL1","Sheet1!A:D");IMPORTRANGE("URL2","Sheet1!A:D")} and then wrap the whole thing in QUERY.

More on QUERY (Google Sheets SQL)

See all →

Formulas used

Written and reviewed by FormulaCraft Team. Each formula on this page is run through our verification engine before publishing.

Last reviewed: