If formula and Indirect formula

Ev1lZer0

New Member
Joined
Sep 26, 2022
Messages
34
Office Version
  1. 365
Platform
  1. Windows
I am trying to get color bands on similar dates while having alternating colors on the rest of the table. I started by using an IF formula and had "=IF(A1=A2,B1,NOT(B1))" I also had come conditional formatting involved.
If you need more information, I am using the process shown on the website. "Colour Bands in Excel Table Based on Dates – Contextures Blog"

It works really well, except for when I insert or delete rows. Which will happen frequently.

When I add rows, I get an inconsistent formula error; when I delete rows I get a #REF error. These errors always start BELOW the inserted row.

---

To resolve this, I tried using an INDIRECT formula with the IF formula and ended up with this. =IF((ADDRESS(ROW()-1,COLUMN()-1))=(ADDRESS(ROW(),COLUMN()-1)),B9,NOT(B9)) it does not work. It is now alternating my true false statement incorrectly. in addition, the [value if false] and [value if true] statement is still referencing the cell above which ends up breaking the formula in the same way I stated earlier.
When I add rows, I get an inconsistent formula error; when I delete rows I get a #REF error. These errors always start BELOW the inserted row.

---

lastly, I tried changing the formula to "=IF((ADDRESS(ROW()-1,COLUMN()-1))=(ADDRESS(ROW(),COLUMN()-1)),(ADDRESS(ROW()-1,COLUMN())),NOT(ADDRESS(ROW()-1,COLUMN())))" My hope was to resolve the #REF error and Inconsistent formula error and address the issue where this formula seems to not correctly label true false.

--

I'm pulling out my hair and desperately need help

The end goal... to alternate color bands based on similar dates like the image below. The true and false should be alternated while similar dates should be grouped together and alternated.

And to state again, the solution needs to work when adding or deleting cells - mine in the past have not.

1664221383066.png
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Hi.

This can be done with (at most) 2 helper columns + 2 conditional formatting rules.
  • You can hide the two helper columns.
  • IF you append/include these two helper columns in the Excel Table itself, then you don't need to carry these formulas down.
  • This conditional formatting will work, even if rows are filtered/hidden.
  • Note: Under the column named "Cells with Conditional Formatting" (specifically, under Cell) in the image below, that's the range that the conditional formatting formula "Applies to". With the Excel Table, when you add a new row, it will automatically change the conditional formatting "Applies to" range. (You just have to set it up just ONCE.)
  • But it appears that if you delete a row, you will have to re-carrydown the formulas in the helper columns. (So if there is indeed no way arround that, either you get someone to write a VBA script for you to unhide the 2 helper columns and recarry down the formula or manually do it after you have deleted all rows that you want to.)
Book1.xlsx
ABCD
1Date of TrialTRUEDate of Trial (Helper)TRUE (Helper)
29/6/2022FALSE12
39/6/2022TRUE23
49/12/2022TRUE14
59/12/2022TRUE25
69/12/2022TRUE36
79/12/2022TRUE47
89/12/2022TRUE58
99/12/2022TRUE69
109/12/2022TRUE710
119/12/2022TRUE811
129/12/2022TRUE912
139/12/2022TRUE1013
149/12/2022TRUE1114
159/12/2022TRUE1215
169/12/2022TRUE1316
179/12/2022TRUE1417
189/19/2022FALSE118
199/19/2022FALSE219
209/20/2022TRUE120
219/21/2022FALSE121
229/22/2022TRUE122
239/23/2022FALSE123
249/26/2022TRUE124
259/26/2022TRUE225
269/26/2022TRUE326
279/26/2022TRUE427
Sheet2 (2)
Cell Formulas
RangeFormula
C2:C27C2=IF(A2=A1,COUNTIF($A$2:A2,A2),1*IF(SUBTOTAL(103,A1)>0,1,0))
D2:D27D2=IF(SUBTOTAL(103,A2)>0,1,0)*ROW()
Cells with Conditional Formatting
CellConditionCell FormatStop If True
B2:B27Expression=MOD(D2,2)=0textNO
A2:A27Expression=MOD(COUNTIF($C$2:C2,1),2)=0textNO
 
Last edited:
Upvote 0
While I'm at it, here is VBA code that will create a button that, when you click on it, it will carry down the formula (in the Date of Trial Helper column) for you. But note that when you click on this button, you cannot undo any action you did before clicking on it! Therefore, if you decide to use it, here is the code and instructions.

  1. See my signature for instructions on how to enable VBA macros/code if you want to use it (and have never used macros/VBA before).

  2. After you do that, press Alt+F11 to bring up the code window.

  3. In the left margin, you will see a list of sheets like this (if not, press Ctrl + R to bring it up).
    Sheets.PNG


  4. Right click on any of those sheets and insert a new module:
    Insert Module.PNG


  5. Copy all of the code in the code block below inside of it.
VBA Code:
Option Explicit

Sub Test__Create_Button()
Call Create_Button("G1:G2", ActiveSheet.Name, "Test__Update_Table")
End Sub

Sub Test__Update_Table()
Call Update_Table("Table3", "Date of Trial", "Date of Trial (Helper)")
End Sub

Sub Create_Button(cellAddress As String, sheetName As String, macroNameToRun As String)

Sheets(sheetName).Buttons.Delete

With Sheets(sheetName).Range(cellAddress)
    Dim btn As Button
    Set btn = Sheets(sheetName).Buttons.Add(.Left, .Top, .Width, .Height)
End With

With btn
    .Font.Size = 12
    .Font.Bold = False
    .Font.Name = "Calibri"
    .OnAction = macroNameToRun
    .Caption = "Fix"
    .Name = sheetName & "_Fix Button_" & cellAddress
End With

End Sub
Sub Update_Table(tableName$, dateOfTrialColumnName$, helperColumnName$)

On Error GoTo Let_User_Know
With Range(tableName)
    Dim headerRow&
    headerRow = .Rows(1).Row - 1

    Dim helperColumnNumber%
    helperColumnNumber = Trim(Evaluate("MATCH(" & Chr(34) & helperColumnName & Chr(34) & "," & headerRow & ":" & headerRow & ",0)"))

    'If at least 1 row was deleted from the table (there is at least one cell with #REF! error in the helper column),
    If Evaluate("SUM(IFERROR(" & tableName & "[" & helperColumnName & "]*0,1))") > 0 Then

        Dim firstRow&
        firstRow = headerRow + 1

        Dim numberOfRowsInTable&
        numberOfRowsInTable = .Rows.Count

        Dim dateOfTrialColumnNumber%
        dateOfTrialColumnNumber = Trim(Evaluate("MATCH(" & Chr(34) & dateOfTrialColumnName & Chr(34) & "," & headerRow & ":" & headerRow & ",0)"))

        Dim colLetter$
        colLetter = Split(Cells(1, dateOfTrialColumnNumber).Address, "$")(1)

        Dim helperColumnRange As Range
        Set helperColumnRange = Range(Cells(firstRow, helperColumnNumber), Cells(firstRow + numberOfRowsInTable - 1, helperColumnNumber))
        helperColumnRange.Formula2 = _
            "=IF(" & colLetter & firstRow & "=" & colLetter & headerRow & ",COUNTIF($" & colLetter & "$" & firstRow & ":" & _
            colLetter & firstRow & "," & colLetter & firstRow & "),1*IF(SUBTOTAL(103," & colLetter & headerRow & ")>0,1,0))"
    End If

End With

Exit Sub
Let_User_Know:
MsgBox "You need to name the table, the trial date heading, and trial date helper column heading what you have them named (verbatim) in this sheet's code module.", vbCritical, "Filling down formula failed."

End Sub

Notes (and final steps):
  • There are 4 Subs in the code block above. You don't need to modify the 3rd and 4th (long) subs.

  • In the first sub, you need to change the "G1:G2" to whatever cell(s) you want the button to be placed on. (Placing it on multiple cells allows to make the button larger.)

  • In the second sub, you need to change the table name from Table3 to whatever the name of your table is in your sheet (keep the quotes).
    Table Name.PNG


  • In the second sub, also be sure that the heading of Date of Trial is whatever you named your "Date of Trial" column in your table (verbatim . . . no Alt+Enter vertical spaces or extra spaces to wrap text, etc.).

  • (But of course, type Date of Trial (Helper). This will ensure that this button will work.)

  • Once all of that is finished, click anywhere on the middle line in the first sub and then click on the green play button to create the button. (If at any time you wish to delete the button, right click on it and select Cut.)
    Create button.PNG
 
Last edited:
Upvote 0
And this reminds me, in order for formatting of Excel Tables to not "conflict" with the conditional formatting, you need to select this (blank) formatting option for the table if it isn't already:
Table Design.PNG


Final Note:
The conditional formatting for column B (TRUE/FALSE) will only work if the first row of data is on an even row number. So if you want to offset the table vertically down from the top, it needs to be that the first data row starts on row 4, 6, etc. (if not where you have it currently at row 2).
 

Attachments

  • 1664253321597.png
    1664253321597.png
    13.3 KB · Views: 2
Upvote 0
Another option without any helper columns
Fluff.xlsm
A
1Date of Trial
209/06/2022
309/06/2022
409/12/2022
509/12/2022
609/12/2022
709/12/2022
809/12/2022
909/12/2022
1009/12/2022
1109/12/2022
1209/12/2022
1309/12/2022
1409/12/2022
1509/12/2022
1609/12/2022
1709/12/2022
1819/09/2022
1919/09/2022
2020/09/2022
2121/09/2022
2222/09/2022
2323/09/2022
2426/09/2022
2526/09/2022
2626/09/2022
2726/09/2022
Main
Cells with Conditional Formatting
CellConditionCell FormatStop If True
A2:A27Expression=MOD(SUMPRODUCT(--($A$2:$A2<>$A$1:$A1)),2)=0textNO
 
Upvote 0
@Fluff , but your solution doesn't hold if rows in the table are filtered. I assume that this is a desired/expected feature (since the table in the OP has filters). Can you modify it so that it does accompany that constraint? (And also make a CF for Column B?)
 
Upvote 0
The OP made no mention of filtering the data & the formula I suggested replicates what the OP is doing with the helper column.
 
Upvote 0
Okay, I guess we will have to wait and see if keeping the CF when the table is filtered is important.
 
Upvote 0
I am going to play with both of these options.

At first glance, it seems that @Fluff's solution may work, but I will have to experiment with the filters to see if keeping the CF is actually important.

To be honest, I hope it's not, since this seems like a much simpler way of 'fixing things' than @cmowla's way. At the end of the day, I am just creating the spreadsheet, it will be others that utilize it on a regular basis. Either direction I go, I am super appreciative of the help and will report back my findings.

Thank you!
 
Upvote 0
If you filter the data the the CF may not work as you want, but it will be the same as the way you were using.
 
Upvote 0

Forum statistics

Threads
1,215,020
Messages
6,122,709
Members
449,093
Latest member
Mnur

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top