Skip to content

Instantly share code, notes, and snippets.

@ExcelRobot
Last active June 8, 2022 01:35
Show Gist options
  • Save ExcelRobot/94510f2745083c389c48fbc5ddf9f79b to your computer and use it in GitHub Desktop.
Save ExcelRobot/94510f2745083c389c48fbc5ddf9f79b to your computer and use it in GitHub Desktop.
Is Formula Consistent LAMBDA Function
/*
Name: IsFormulaConsistent (IsFormulaConsistent)
Description: Given a cell, checks to make sure the cell above and below have
consistent formulas. Uses FORMULATEXT and compares the formulas to make
sure they are the same except for numbers that increase/decrease by 1.
Optional parameter to specify the cell to compare with. This function is
recursive. Can be used with conditional formatting to highlight cells that
have inconsistent formulas.
Author: Excel Robot (@ExcelRobot)
Category: Auditing
*/
IsFormulaConsistent = LAMBDA(Cell,[WithCell], LET(
FirstFormula, FORMULATEXT(Cell),
SecondFormula, FORMULATEXT(IF(ISOMITTED(WithCell),OFFSET(Cell,-1,0),WithCell)),
NotBothFormulas, OR(ISERROR(FirstFormula),ISERROR(SecondFormula)),
RowDiff, IF(ISOMITTED(WithCell),-1,ROW(WithCell)-ROW(Cell)),
CharactersFromFirstFormula, UPPER(MID(FirstFormula,SEQUENCE(1,LEN(FirstFormula),1),1)),
CharactersFromSecondFormula, UPPER(MID(SecondFormula,SEQUENCE(1,LEN(SecondFormula),1),1)),
AsciiCode0, CODE("0"),
AsciiCode9, CODE("9"),
NumbersFromFirstFormula, FILTERXML("<r><c>"&SUBSTITUTE(TRIM(TEXTJOIN("",TRUE,IF((CODE(CharactersFromFirstFormula)>=AsciiCode0)*(CODE(CharactersFromFirstFormula)<=AsciiCode9),CharactersFromFirstFormula," ")))," ","</c><c>")&"</c></r>","//c"),
NumbersFromSecondFormula, FILTERXML("<r><c>"&SUBSTITUTE(TRIM(TEXTJOIN("",TRUE,IF((CODE(CharactersFromSecondFormula)>=AsciiCode0)*(CODE(CharactersFromSecondFormula)<=AsciiCode9),CharactersFromSecondFormula," ")))," ","</c><c>")&"</c></r>","//c"),
TextFromFirstFormula, TEXTJOIN("",TRUE,IF((CODE(CharactersFromFirstFormula)>=AsciiCode0)*(CODE(CharactersFromFirstFormula)<=AsciiCode9),"",CharactersFromFirstFormula)),
TextFromSecondFormula, TEXTJOIN("",TRUE,IF((CODE(CharactersFromSecondFormula)>=AsciiCode0)*(CODE(CharactersFromSecondFormula)<=AsciiCode9),"",CharactersFromSecondFormula)),
IsMatch, IFERROR(AND(TextFromFirstFormula=TextFromSecondFormula,AND((IFERROR(NumbersFromFirstFormula+RowDiff,"")=IFERROR(NumbersFromSecondFormula,""))+(IFERROR(NumbersFromFirstFormula,"")=IFERROR(NumbersFromSecondFormula,"")))),False),
MatchesCellBelow, IF(ISOMITTED(WithCell),IsFormulaConsistent(Cell,OFFSET(Cell,1,0)),TRUE),
HasNoLetters, IFERROR(LEN(TEXTJOIN("",TRUE,IF((CODE(CharactersFromFirstFormula)>=CODE("A"))*(CODE(CharactersFromFirstFormula)<=CODE("Z")),CharactersFromFirstFormula,"")))=0,TRUE),
IsConsistent, IF(HasNoLetters,FALSE,IF(NotBothFormulas,TRUE,IF(ISOMITTED(WithCell),AND(IsMatch,MatchesCellBelow),IsMatch))),
IsConsistent
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment