Restrict macro to run n times during specified period

delta_negative

New Member
Joined
Mar 11, 2013
Messages
37
Is there a way to restrict a macro to run only a specified number of times during a user designated time period, for example run no more than 3 times during a 24 hour period?

We have a macro that runs to alert maintenance personnel if a certain temperature condition is met in machinery. This condition may happen numerous times during a day but we only need to alert the mechanics once a day if the condition is true.

Thanks for any assistance you can provide.
 
Last edited:

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result

Snakehips

Well-known Member
Joined
May 17, 2009
Messages
5,550
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
delta_negative,

Perhaps something like this....

You will need 3 spare cells anywhere in the workbook. I have coded as Cells A1:A3 in sheet "SomeSheet"

In A1 enter the number of times you want to alert in a given day.
A2 will hold the current date, programatically.
A3 will hold count of alerts to date, programatically.

Rich (BB code):
Sub Alert()
'Reset date& count if new day
If Not Sheets("SomeSheet").Range("A2") = Date Then
Sheets("SomeSheet").Range("A2") = Date
Sheets("SomeSheet").Range("A3") = 0
Else
'Check count
If Sheets("SomeSheet").Range("A3") >= Sheets("SomeSheet").Range("A1") Then Exit Sub


'Do your existing Alert stuff then increment count

Sheets("SomeSheet").Range("A3") = Sheets("SomeSheet").Range("A3") + 1
Exit Sub
End If
End Sub

Hope that helps.
 
Last edited:

delta_negative

New Member
Joined
Mar 11, 2013
Messages
37
delta_negative,

Perhaps something like this....

You will need 3 spare cells anywhere in the workbook. I have coded as Cells A1:A3 in sheet "SomeSheet"

In A1 enter the number of times you want to alert in a given day.
A2 will hold the current date, programatically.
A3 will hold count of alerts to date, programatically.

Rich (BB code):
Sub Alert()
'Reset date& count if new day
If Not Sheets("SomeSheet").Range("A2") = Date Then
Sheets("SomeSheet").Range("A2") = Date
Sheets("SomeSheet").Range("A3") = 0
Else
'Check count
If Sheets("SomeSheet").Range("A3") >= Sheets("SomeSheet").Range("A1") Then Exit Sub


'Do your existing Alert stuff then increment count

Sheets("SomeSheet").Range("A3") = Sheets("SomeSheet").Range("A3") + 1
Exit Sub
End If
End Sub

Hope that helps.

Thank you very much for responding.

Will try the code tomorrow and let you know how it goes.
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,526
Messages
5,832,268
Members
430,124
Latest member
Potatokat

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