VBA Not Application.Intersect + Worksheet_Calculate

bnichols

New Member
Joined
Dec 28, 2022
Messages
14
Office Version
  1. 365
Platform
  1. MacOS
This worked about a week ago, but it stopped for some reason. I have a checkbox and the below code is supposed to trigger when the cell N89 changes (changes via a formula). Right now, when I try and check the box, it triggers the code and unchecks it. I tried disabling the true/false enable evens and calculation manual/auto to see if it fixed it, but it didn't. Any advice is appreciated!

VBA Code:
Private Sub Worksheet_Calculate()

Set ws = ThisWorkbook.ActiveSheet
     If ws.Name Like "Period*" Then

    ActiveWorkbook.Save
    On Error GoTo ErrorHandler
    'Err.Raise vbObjectError + 1000
    With Application
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
        .DisplayStatusBar = False
         .EnableEvents = False

        Dim Target As Range
        Set Target = Range("N89")
        Set cboxVariance = ws.CheckBoxes("Variance Wk1")
        Set KeyCellsV = Range("N89")

       If Not Application.Intersect(KeyCellsV, Range(Target.Address)) _
       Is Nothing Then
    cboxVariance.Value = xlOff

    End If
End With
ErrorExit:
With Application
    .Calculation = xlCalculationAutomatic
    .ScreenUpdating = True
    .DisplayStatusBar = True
      .EnableEvents = True
    Exit Sub

ErrorHandler:
    MsgBox "The following Error occurred: " & Err.Description
    Resume ErrorExit

End With
End If
End Sub
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
It looks like the code is checking the value of cell N89 and then setting the value of the checkbox "Variance Wk1" to xlOff (unchecked) if the value of cell N89 has changed. The issue you're experiencing is likely caused by the code running in an infinite loop, where the checkbox is being unchecked, which causes the Worksheet_Calculate event to trigger again, which unchecks the checkbox again, and so on. To fix this issue, you can try adding a check to see if the checkbox is already unchecked before running the code that unchecks it.

VBA Code:
If cboxVariance.Value = xlOff Then Exit Sub
cboxVariance.Value = xlOff


This will ensure that the code only runs when the checkbox is checked and will prevent the infinite loop.

Another solution could be that the checkbox is being updated by another function, making the Worksheet_Calculate function redundant and unnecessary.

VBA Code:
Private Sub Worksheet_Calculate()

' Assign the active sheet to a variable
Set ws = ThisWorkbook.ActiveSheet
     If ws.Name Like "Period*" Then

    ' Save the workbook
    ActiveWorkbook.Save
    On Error GoTo ErrorHandler

    ' Turn off calculation, screen updating, and enable events
    With Application
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
        .DisplayStatusBar = False
        .EnableEvents = False
    End With
    
    ' Declare variables for the target range and checkbox
    Dim Target As Range
    Set Target = Range("N89")
    Set cboxVariance = ws.CheckBoxes("Variance Wk1")
    Set KeyCellsV = Range("N89")
    
    ' Check if the target range intersects with the key cells
    If Not Application.Intersect(KeyCellsV, Range(Target.Address)) _
    Is Nothing Then
        ' Check if the checkbox is already unchecked
        If cboxVariance.Value = xlOff Then Exit Sub
        cboxVariance.Value = xlOff
    End If

ErrorExit:
    ' Turn on calculation, screen updating, and enable events
    With Application
        .Calculation = xlCalculationAutomatic
        .ScreenUpdating = True
        .DisplayStatusBar = True
        .EnableEvents = True
        Exit Sub
    End With

ErrorHandler:
    ' Display error message
    MsgBox "The following Error occurred: " & Err.Description
    Resume ErrorExit

End If
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,971
Messages
6,122,520
Members
449,088
Latest member
RandomExceller01

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