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

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
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:
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,222,194
Messages
6,164,508
Members
451,900
Latest member
lamski

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