Google Sheets Conditional Formatting: Invalid Formula
Fix the Invalid formula error in Google Sheets conditional formatting. Use INDIRECT for other sheets, and check curly quotes and locale separators.
本文目录
Your custom formula works in a Google Sheets cell, but Custom formula is rejects it as invalid. Or the rule saves, and none of the cells change color.
A stock table you can rebuild
Create a sheet named Orders and enter this table in A1:D8.
| SKU | Region | Qty | Reorder level |
|---|---|---|---|
| KB-101 | East | 620 | 300 |
| MS-204 | West | 180 | 200 |
| HD-310 | East | 450 | 250 |
| CB-115 | North | 780 | 400 |
| MN-220 | East | 540 | 300 |
| AD-108 | West | 90 | 150 |
| USB-305 | South | 500 | 200 |
On a second sheet named Settings, enter 500 in A1.
If the formula points at another sheet, start there. This comparison is valid in an ordinary cell on Orders and is rejected as a conditional formatting rule:
=$C2>=Settings!$A$1
Use INDIRECT instead:
=$C2>=INDIRECT("Settings!A1")
Select Orders!A2:D8, open Format > Conditional formatting, choose “Custom formula is”, and enter the INDIRECT formula. The rule highlights rows 2, 5, 6, and 8: KB-101, CB-115, MN-220, and USB-305. Changing Settings!A1 from 500 to 600 leaves only rows 2 and 5 highlighted.
The same comparison with a direct Settings!$A$1 reference returns those same TRUE and FALSE values when you enter it in column E and fill it down. It is rejected when you try to use it as the custom formula for the rule.
When the other sheet name has a space
Create a third sheet named Stock List and enter these SKUs in A1:A6.
| SKU |
|---|
| KB-101 |
| HD-310 |
| MN-220 |
| USB-305 |
| SP-410 |
This COUNTIF is also valid in an ordinary cell and is rejected as a custom formatting formula, because of the direct reference to Stock List:
=COUNTIF('Stock List'!A2:A,$A2)>0
Use this version instead, applied to A2:D8:
=COUNTIF(INDIRECT("'Stock List'!A2:A"),$A2)>0
It highlights rows 2, 4, 6, and 8, the four orders whose SKUs appear on Stock List. COUNTIF returns 1 for KB-101 and 0 for MS-204; the >0 turns those counts into TRUE and FALSE. A duplicate SKU on the list would still satisfy >0.
The address passed to INDIRECT uses two kinds of quotation marks: double quotes around the whole address, and single quotes around the sheet name because that name contains a space.
A blank order SKU does not get picked up by this formula. In this test, COUNTIF of a genuine blank cell against Stock List!A2:A returned 0, so >0 was already FALSE. That is not the same as COUNTIF(range,""), which did count the empty cells in the same open-ended column (994 of them). The blank lookup value did not match those empties.
INDIRECT does not walk down the sheet
These two expressions both return 500 on every row of the sample:
=INDIRECT("Settings!A1")
=INDIRECT("Settings!$A$1")
The dollar signs inside the quoted address do not matter. The address is text, so Google Sheets does not advance it to A2, A3, or A4 as the rule moves down the applied range. The COUNTIF example avoids that problem by keeping the lookup range fixed and letting $A2, the SKU being checked, change for each row. For a row-by-row comparison against another sheet, I would bring the values onto this sheet with a helper column instead of building a changing address inside INDIRECT.
Named ranges are not a shortcut in the rule itself
A named range such as Threshold, pointing at Settings!A1, works in an ordinary cell:
=$C2>=Threshold
That formula returns the same seven TRUE and FALSE values as the INDIRECT comparison. As a custom formatting formula it is rejected, and so is a named range that points at a cell on the same sheet. What did work in the rule is wrapping the name in INDIRECT:
=$C2>=INDIRECT("Threshold")
Applied to A2:D8, it highlights rows 2, 5, 6, and 8. I would still rather use a helper column when several rules depend on another sheet, for the rename problem at the end of this article.
Curly quotation marks
If you have already switched to INDIRECT and the custom formula is still rejected, look at the quotation marks. This version uses curly quotes and is not valid spreadsheet syntax:
=$C2>=INDIRECT(“Settings!A1”)
In an ordinary cell it returns #ERROR!, with the message “Formula parse error.” As a custom formatting formula it is rejected. Type straight double quotes from the keyboard:
=$C2>=INDIRECT("Settings!A1")
That formula returns TRUE for row 2 and, applied to A2:D8, highlights rows 2, 5, 6, and 8. Curly quotes often arrive when you paste a formula from a word processor, a messaging app, or a formatted web page. If the sheet name has spaces, check the single quotes around that name as well.
Missing equals, quoted formulas, and non-Boolean results
A custom formula needs a leading = and a condition that evaluates to TRUE or FALSE. These two entries are rejected as custom formulas:
$C2>=$D2
"=$C2>=$D2"
Enter the comparison directly:
=$C2>=$D2
Applied to A2:D8, that rule highlights rows 2, 4, 5, 6, and 8, the orders whose quantities meet or exceed their own reorder levels.
A related mistake is a calculation with no comparison:
=$C2-$D2
In this sample the differences are 320, -20, 200, 380, 240, -60, and 300. The custom formula is accepted, and it highlights every one of those seven rows, because a nonzero number is treated as a match. A negative result such as -20 is highlighted just as a positive 320 is. In a separate type check, 0, FALSE, the text hello, an empty string, and a blank cell were not highlighted.
If the aim is to flag orders below their reorder level, write the comparison:
=$C2-$D2<0
That returns TRUE only for MS-204 and AD-108, and highlights rows 3 and 7.
Commas and semicolons follow the spreadsheet locale
A formula copied from another spreadsheet may use semicolons between function arguments instead of commas. For highlighting East-region orders with a quantity of at least 500, these two are the same condition:
=AND($B2="East",$C2>=500)
=AND($B2="East";$C2>=500)
With the separator the spreadsheet expects, the rule highlights rows 2 and 6, KB-101 and MN-220.
The locale belongs to the spreadsheet, under File > Settings, not just to your Google account or browser language. In a US-locale file, the semicolon formula was accepted both in a cell and as a custom formatting rule, then stored with commas, and it highlighted those two rows. In a German-locale file the direction is not symmetric: the comma formula returned #ERROR!, “Formula parse error,” in a cell and was rejected as a custom formula, while the semicolon formula was stored with semicolons, returned TRUE on rows 2 and 6, and highlighted those rows.
If the same formula also fails in an ordinary cell, see Formula parse error in Google Sheets for other syntax problems, including curly quotes and Excel syntax.
Formulas pasted from Excel
A custom formula pasted from Excel may include a structured table reference or a function prefix that Google Sheets does not evaluate the same way. With no table named Table1 in the file, these two returned #ERROR!, “Formula parse error,” in ordinary cells and were rejected as custom formulas:
=Table1[Qty]>=500
=[@Qty]>=500
I did not test those references against a real Google Sheets table. For this Orders sample, use ordinary cell references:
=$C2>=500
Applied to A2:D8, that highlights rows 2, 5, 6, and 8.
A formula with an Excel-only prefix can fail in a different way. =_xlfn.XLOOKUP(1,A1,A1) returned #NAME? in a cell, with the message “Unknown function: ‘_xlfn.XLOOKUP’.” As a custom formatting rule the same kind of formula was accepted, but it highlighted no cells. A rule that saves is not proof that it is highlighting the rows you expect.
Wrong rows, drifting columns, and text quantities
Once the formula is accepted, the usual problem is the relationship between the formula and the applied range, or the type of the values being compared.
Starting one row too high
If “Apply to range” is A2:D8, write the formula for row 2, not row 1. This formula is valid, but it is aimed one row too high:
=$C1>=500
Row 3, MS-204, is evaluated against C2, which contains 620, so that row is highlighted even though its own quantity is 180. Row 2 is evaluated against C1, the header Qty. In this test "Qty">=500 returned TRUE, so row 2 was highlighted as well. Rows 6 and 7 were also highlighted, because they picked up 780 and 540 from the rows above them. The intended formula is:
=$C2>=500
Column references that drift
When you want the whole row highlighted, this formula is missing the dollar sign before C:
=C2>=500
The column moves as the rule is evaluated across A2:D8. In row 2, A2 is tested against C2 (620), which meets the condition, but B2 is tested against D2 (300), which does not. Only column A of rows 2, 5, 6, and 8 is highlighted. Lock the column and leave the row relative:
=$C2>=500
Every cell in row 2 then uses the quantity in C2, so the whole row highlights. For more on anchoring a rule to the first cell of the applied range, see Conditional formatting formula not working 与 Conditional formatting based on another cell.
Quantities stored as text
Imported quantities sometimes look like numbers but are stored as text. To reproduce this, type a leading apostrophe before the 180 in C3. The cell still displays 180.
=ISNUMBER(C3)
That returns FALSE. The same check on the numeric 620 in C2 returns TRUE. Google Sheets does not compare that text 180 as the number 180: =$C3>=500 returns TRUE, and a =$C2>=500 rule applied to A2:D8 highlights MS-204 along with the four genuine matches. After C3 is converted back to the number 180, the comparison returns FALSE and the highlight on that row disappears.
If you only want genuine numeric quantities, add an ISNUMBER check:
=AND(ISNUMBER($C2),$C2>=500)
Applied to A2:D8 while C3 still held text 180, this highlighted rows 2, 5, 6, and 8 and skipped the text quantity. The guard does not convert text into numbers. Fix the imported values if those quantities should take part in the comparison.
Helper columns survive a renamed sheet
For a single threshold on another sheet, INDIRECT is a reasonable fix. On a shared spreadsheet with several cross-sheet rules, I would rather use a visible helper column.
In E2, enter the direct comparison and fill it down through E8:
=$C2>=Settings!$A$1
Direct cross-sheet references are allowed here, because these are ordinary worksheet formulas. With the original sample and a threshold of 500, E2:E8 contains TRUE, FALSE, FALSE, TRUE, TRUE, FALSE, and TRUE. Then apply this custom formula to A2:D8:
=$E2=TRUE
It highlights rows 2, 5, 6, and 8, and you can see why each row was included without opening the rule editor.
INDIRECT has a maintenance cost that the helper column does not. If someone renames Settings to Rules, Google Sheets does not update the address stored inside "Settings!A1". After that rename, the ordinary formula =$C2>=Settings!$A$1 became =$C2>=Rules!$A$1 and still returned the original Booleans. =INDIRECT("Settings!A1") still contained the old sheet name and returned #REF!, with the message “Function INDIRECT parameter 1 value is ‘Settings!A1’. It is not a valid cell/range reference.” The custom formula that used that INDIRECT expression stayed saved and highlighted no cells.
A named range still resolved after the rename, and =INDIRECT("Threshold") kept highlighting rows 2, 5, 6, and 8. The helper column of direct references also kept working, because those references were updated with the tab name.
If you are already troubleshooting a rule, put its condition into an unused cell on the first row of the applied range and fill it down. A formula that returns the right Booleans in ordinary cells, but is rejected as a custom formula, is hitting a restriction in the rule. A formula that returns the wrong values in those cells needs its logic or references corrected before formatting enters the picture.