I have this module here that has is Workbook subroutine. I can’t for the life of me understand how the GenerateLimitSummary is ever able to run? Can someone please articulate the process flow here?
Private LimitBool As Boolean
Private Sub Workbook_SheetCalculate(ByVal Sh As Object)
If LimitBool Then Exit Sub
' use conditional formatting to highlight limit breaches
ApplyConditionalFormatting
' regenerate the summary limits sheet
LimitBool = True
GenerateLimitSummary
LimitBool = False
End Sub
The author uses LimitBool to prevent a infinite loop/a stack overflow:
LimitBoolisFalse, therefore the remainder ofWorkbook_SheetCalculateis executedLimitBoolis set toTrue(after it was confirmed it’s notTrue)GenerateLimitSummaryis executed. If this routine now for some reasons forces the workbook to recalculate,Workbook_SheetCalculatewill be triggered again. However, asLimitBoolis nowTrue*, the second call to this procedure is now Exited after the first check. If it would not have this check, it would again callGenerateLimitSummary, which would then trigger the recalc, etc…GenerateLimitSummaryran,LimitBoolis set back toFalse, therefore, it can ran again(*) – it has a Module-wide scope, i.e. it keeps it value across the different calls, while a procedure-wide scope (=Dimmed in the sub) would create a new variable for each call