Creating a Stop for Worksheet Calculate Event

rickincanada

Board Regular
Joined
Aug 31, 2010
Messages
61
Hi there!

I'm a fairly new VBA user and am attempting use the Worksheet Calculate Event to fire an Advanced Filter macro that I have. My trouble is that once the worksheet calculates, the macro runs which then causes the worksheet to calculate again creating an infinite loop.

Here's what I was thinking I could do however it isn't working...

Private Sub Worksheet_Calculate() 'using worksheet "Deal Info"
If Range("B36").Value = 0 Then
Call OptionFilter
Range("B36").Value = 1
End If
Range("B36").Value = 0
End Sub

As you can see I'm trying to get the function to look at a cell value and if it is 0 then run and if it doesn't then stop. I'd run the macro and then upon completion place a 1 in the cell that is designed to stop it. I can see how this logic fails however I'm not sure how to best complete this task.

Please help! Thanks.
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.

Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
8,332
Office Version
  1. 2016
Platform
  1. Windows
Hi there!

I'm a fairly new VBA user and am attempting use the Worksheet Calculate Event to fire an Advanced Filter macro that I have. My trouble is that once the worksheet calculates, the macro runs which then causes the worksheet to calculate again creating an infinite loop.

Here's what I was thinking I could do however it isn't working...

Private Sub Worksheet_Calculate() 'using worksheet "Deal Info"
If Range("B36").Value = 0 Then
Call OptionFilter
Range("B36").Value = 1
End If
Range("B36").Value = 0
End Sub

As you can see I'm trying to get the function to look at a cell value and if it is 0 then run and if it doesn't then stop. I'd run the macro and then upon completion place a 1 in the cell that is designed to stop it. I can see how this logic fails however I'm not sure how to best complete this task.

Please help! Thanks.

Does this work for you ?

Code:
Private Sub Worksheet_Calculate() 'using worksheet "Deal Info"
[B]Application.EnableEvents=False[/B]
If Range("B36").Value = 0 Then
Call OptionFilter
Range("B36").Value = 1
End If
Range("B36").Value = 0
[B]Application.EnableEvents=True[/B]
End Sub
 

Forum statistics

Threads
1,143,656
Messages
5,720,116
Members
422,266
Latest member
Mattyw

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
Top