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

Try something like this (barely tested),

Private Sub Workbook_Open()
Dim ymNow As Long, ymLast As Long
Dim nm As Name

ymNow = Year(Date) & Right("0" & Month(Date) + 1, 2)
On Error Resume Next
Set nm = ThisWorkbook.Names("LastCheck")
If Err.Number Then
Set nm = ThisWorkbook.Names.Add("LastCheck", "=" & ymNow)
nm.Visible = False ' hide from user
Else
ymLast = Val(Right(nm, 6))
End If
On Error GoTo 0

If ymLast < ymNow Then
nm.RefersTo = "=" & ymNow ' update
' call DoStuff ' your macro
End If
End Sub

Obvoiously you could store ymNow in a cell somewhere, hidden perhaps, rather
than a Name.

Not quite sure how you want to handle the scenario of a skipped month, but
hopefully you can adapt the above.

Regards,
Peter T

"Wes_A" wrote in message
...
XP Pro / Excel 2007
I need to run a macro once only each month when the workbook is opened for
the first time in the new month. i.e. It should not run again in the same
month.
However, it should not skip a month in the (unlikely) event that the
workbook is not opened at all during that month and is only opened in the
month following......
Can anyone assist with code for this one?