- last edited on
- last edited on
Using two different versions of 32-bit Excel 2016 (16.0.4549.1000 at home and 16.0.8201.2207 at work) on Win 10,
I have a table with headers starting at A1 that retrieves data from an sql database.
I select the table body rows, say A2:C5 and apply conditional formatting to color cells with =(A1=A2), so a cell is highlighted whenever it matches the cell above.
All works great as I refresh the query, unless the query result is ever empty -- then the conditional formatting breaks in one of two ways: the A1 reference become #REF! or becomes A1048574.
How do I prevent this, or fix it without manually repairing the conditional formatting each time?
(VBA workarounds would be fine.)
To reproduce, you can make a query to an Excel file:
Make a small table in excel, save as tmp.xlsx. Then Data>Get External>From Other>Microsoft Query, select Excel Files. In the picker, check Read only and point to tmp.xlsx. Options>check systems tables so you can select Sheet1$ as the input table, click * to get all data, and File>Return to Excel, and choose to put the table at A1. You now have a query table that retrieves its data from tmp.xlsx
Select the table body (not the headers) and add conditional formatting rule to fill cells with =(A1=A2), i.e., if it matches the cell above.
Delete the table rows, then right click in the table and select Refresh.
The conditional format rule now reads =(A1048574=A2)
Fix it, then delete the data lines in tmp.xlsx, leaving the headers, and save. Refresh the query table and the conditional formatting rule reads =(#REF!=A2). Add data back and refresh again, and it remains broken.
12-14-2017 09:49 AM
12-14-2017 09:49 AMSolution
With resulting table use structured references for the conditional formatting. It doesn't work with them directly, the workaround is to use INDIRECT. If the name of your resulting table is Tbl and table column name is "a" when like
instead of =A1=A2