3

I've run into a Stack Overflow issue with the below code. The error happened on Set rngToCheck = Range("GenLoanAmount") and I'm not really sure why since there doesn't seem to be enough happening that would cause that issue. This code does work, so if deemed necessary by the community I will post it on Code Review. Thank you for taking a look.

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rngToCheck As Range
    Set rngToCheck = Range("GenLoanAmount")


    If Not Intersect(Target, Me.Range("FloodIns")) Is Nothing Then
        If Intersect(Target, Me.Range("FloodIns")) = "Y" Or Intersect(Target, Me.Range("FloodIns")) = "y" Then FloodEmail.Show
    End If

    If Not Intersect(Target, Me.Range("FloodInsAct")) Is Nothing Then
        If Intersect(Target, Me.Range("FloodInsAct")) = "Y" Or Intersect(Target, Me.Range("FloodInsAct")) = "y" Then FloodActEmail.Show
    End If

    If Not Intersect(Target, rngToCheck) Is Nothing Then
        If Intersect(Target, rngToCheck) Then
            rngToCheck.NumberFormat = "$#,##0.00"
        End If
    End If

    If Not Intersect(Target, Me.Range("genCloseDate")) Is Nothing Then
        If Intersect(Target, Me.Range("genCloseDate")) <> vbNullString Then FundDateCalc
    End If


End Sub
6
  • When you say the code does work, do you mean it only works when you don't get the error, or that it works whether or not you get the error? Commented Apr 3, 2019 at 20:11
  • 1
    Also, on which line is the error occurring? Commented Apr 3, 2019 at 20:12
  • 1
    Does this code, specifically FundDateCalc, trigger a change in the worksheet? To be safe, events should be disabled and then enabled at the end. Commented Apr 3, 2019 at 20:13
  • @Mistella, I added the line of code that the debugger highlighted, and it works unless i get the error then Excel just crashes. Commented Apr 3, 2019 at 20:15
  • 1
    See the posted answer :) Commented Apr 3, 2019 at 20:17

1 Answer 1

2

This procedure handles the Worksheet.Change event, which Excel fires whenever a cell changes on that worksheet.

The handler changes cells on that sheet (or invokes code that makes changes on that sheet), and is therefore re-entrant. Disable events at the start of the procedure, and re-enable them before the end of it, to avoid recursing until the call stack can't take it anymore (i.e. until you get a "stack overflow" error).

Dim wasEnabled As Boolean
wasEnabled = Application.EnableEvents

Application.EnableEvents = False

'...code...

Application.EnableEvents = wasEnabled
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! Whats sad is that i have used Application.EnableEvents so many times before.
@ZackE on the bright side, you've contributed a "stack overflow" question to Stack Overflow, pretty legit if you ask me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.