View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
OssieMac OssieMac is offline
external usenet poster
 
Posts: 2,510
Default Date dependent macro - run once only

The following code goes in ThisWorkbook module. You need either a special
worksheet or a cell in an existing worksheet where you can store the End of
Month Date for the last month that the macro is run. Manually Initialize this
cell with the End of Month date of this month. (I have used a worksheet
called 'Run Log'.)

When the code runs, it increments the date in the cell to the next End of
Month date. Using the Do Loop, if that date is still before the today's date
then the code runs again and then increments the date again to the next End
of Month date and therefore if one month is missed then it will run twice or
for as many times as months missed.

Note that a space and underscore at the end of a line is a line break in an
otherwise single line of code.

See notes at end of post for testing method.

Private Sub Workbook_Open()

Dim dateEOM As Date

dateEOM = Date 'Today's date

Do While dateEOM Worksheets("Run Log") _
.Range("A1")

'Insert your code here in lieu of MsgBox
MsgBox "Run for " & _
Worksheets("Run Log").Range("A1")

Range("A1") = WorksheetFunction.EoMonth _
(Worksheets("Run Log").Range("A1"), 1)
Loop

End Sub

I specifically assigned today's date to a variable so that you can test the
code by first inserting the last day of current month in the cell and then
edit the following line to add 99 days to today's date and you will see the
results with the MsgBox.

dateEOM = Date + 99 'Today's date plus 99 for testing.

--
Regards,

OssieMac