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:

Some videos you may like

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.

Snakehips

Well-known Member
Joined
May 17, 2009
Messages
5,069
Office Version
  1. 2013
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.
 

Watch MrExcel Video

Forum statistics

Threads
1,122,214
Messages
5,594,876
Members
413,946
Latest member
richelg

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