Question

I'm having trouble figuring out a fairly simple QUERY statement in Google Spreadsheets. I'm trying to use a cell reference instead of static values and I'm running into trouble. Below it the code I'm using, but I keep getting a "Error: Formula parse error."

=QUERY(Responses!B1:I, "Select B where G contains"& $B1 &)

I'm sure it is a simple error, but can someone please show me how to write the above so the QUERY is pulling data from B where G contains the value in cell B1 (cell reference)?

Was it helpful?

Solution 2

I only have a workaround here. In this special case, I would use the FILTER function instead of QUERY:

=FILTER(Responses!B:B,Responses!G:G=B1)

Assuming that your data is on the "Responses" sheet, but your condition (cell reference) is in the actual sheet's B1 cell.

Hope it helps.

UPDATE:

After some search for the original question: The problem with your formula is definitely the second & sign which assumes that you would like to concatenate something more to your WHERE statement. Try to remove it. If it still doesn't work, then try this:

=QUERY(Responses!B1:I, "Select B where G matches '^.\*($" & B1 & ").\*$'") - I have not tried it, but it helped in another post: Query with range of values for WHERE clause?

OTHER TIPS

Copied from Web Applications:

=QUERY(Responses!B1:I, "Select B where G contains '"&$B1&"'")

I know this is an old thread but I had the same question as the OP and found the answer:

You are nearly there, the way you can include cell references in query language is to wrap the entire thing in speech marks. Because the whole query is written in speech marks you will need to alternate between ' and " as shown below.

What you would need is this:

=QUERY(Responses!B1:I, "Select B where G contains '"& B1 &"' ")

If you then wanted to refer to multiple cells you could add more like this

=QUERY(Responses!B1:I, "Select B where G contains '"& B1 &"' and G contains '"& B2 &"' ")

The above would filter down your results further based on the contents of B1 and B2.

I found out that single quote > double quote > wrapped in ampersands did work. So, for me it looks like this:

=QUERY('Youth Conference Registration'!C:Y,"select C where Y = '"&A1&"'", 0)

Here is working code: =QUERY(Sheet1!$A1:$B581, "select B where A = '"&A1&"'")

In this scenario I needed the interval to stay fixed and the reference value to change when I drag it.

none of the above answers worked for me. This one did:

=QUERY(Copy!A1:AP, "select AP, E, F, AO where AP="&E1&" ",1)

To make it work with both text and numbers:

Exact match:

=query(D:E,"select * where D like '"&C1&"'", 0)

Convert search string to lowercase:

=query(D:E,"select * where D like lower('"&C1&"')", 0)

Convert to lowercase and contain part of the search string:

=query(D:E,"select * where D like lower('%"&C1&"%')", 0)

enter image description here

A1                = query/formula
yellow / A:B  = result area
green / C1    = search area
blue / D:E     = data area

If you get error when the input is text and not numbers; move the data and delete the (now empty) columns. Then move the data back.

Old Thread but I found this on my journey to the below answer and figure someone else might need it too.

=IFERROR(ArrayFormula(query(index(Sheet3!A:C&""),"select* Where Col1="""&B1&""" ")), ARRAYFORMULA({"*     *","no cells","match"}));

Here is a simply built text Filter from a 3 column data set (A,B and C) located in "sheet3" into the current sheet and calling a comparison to a cell from the current sheet to filter within Col1(A).

This bit is just to get rid of the #N/A error if the filter turns up no results //ARRAYFORMULA({"* *","no cells","match"}))

Your question-title is very broad despite the question-body is asking for only one type of 'cell reference'. I'll try to answer some of the other scenarios in case other people come here looking for them.

A - String comparison with cell reference

=TRANSPOSE(QUERY(MIX!A16:F, "SELECT B,C,D,E,F   WHERE  (UPPER(A) = '"&F1&"')  "))

=QUERY(MIX!A16:F, "SELECT B,C,D,E,F   WHERE  (UPPER(A) = '"&F1&"')  ")

F1 = StringToSearch

B - String comparison with cell reference + SheetName is called using cell reference + Dates comparison using cell reference

F6  =  SheetName
F2 and F3  =  StringToSearch
I4 and I5 = dates with this format =  2022-01-01 = yyyy-mm-dd

If the dates you have are in a different format, you can use the formula below to convert them

I4 = TEXT(H5,"yyyy-mm-dd")


=QUERY(INDIRECT(F6&"!A2:C") , "select A,B,C where ((upper(B) contains '"&F2&"') or  (upper(B) contains '"&F3&"'))  AND (A >= date '"&I4&"')  AND (A <= date '"&I5&"')       ")

C - Data to query comes completely using cell reference

B1 = Sheetname!Range  =  Sheet1!A1:C

A36 = stringToFind


=QUERY(INDIRECT($B$1),"SELECT * WHERE upper(B) CONTAINS upper('"&A36&"')  ")


The "$" in $B$1 make the reference static for column and row, so if you copy/paste the CELL containing the formula (NOT the text of the formula), the A36 adapts accordingly, but B1 stays fixed.

D - Data to query comes completely using cell reference + String comparison using cell reference + Number comparison using cell reference

=QUERY(INDIRECT($B$1),"SELECT * WHERE upper(B) CONTAINS upper('"&A40&"') AND C > "&A41&" ")


B1 = Sheetname!Range  =  Sheet1!A1:C
A40 = stringToFind = blah
A41 = Number = 100

E - Nested query (query inside query) + String comparison using cell reference + Number comparison using cell reference, using LIMIT and OFFSET

=QUERY( transpose( query(IMPORT!$A$7:AAL,"select *  where A contains '"&$C34&"' "))  ,"SELECT * LIMIT " & $A34 & " OFFSET " & $B34, 0 ) 

The final '0' is there to NOT include the header in the results.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top