Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 37
Default Date dependent macro - run once only

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?
  #2   Report Post  
Posted to microsoft.public.excel.programming
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?



  #3   Report Post  
Posted to microsoft.public.excel.programming
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


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 140
Default Date dependent macro - run once only

What I would do is;

1. Setup a last run month cell. It contains the month number of the last
time the RunOnce macro ran.
2. At the beginning of the Workbook_Open macro, check the value in your last
run month cell.
If the value is different from this month's number, then trigger
your RunOnce macro.
3. At the end of your RunOnce macro, update your last run month cell.

"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?


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Date-dependent calculation Steve O'Donnell Excel Worksheet Functions 2 March 28th 07 02:50 AM
Results dependent on date RaV Excel Discussion (Misc queries) 1 February 14th 06 04:39 PM
Dependent Macro/Toggle Buttons Chiku Excel Programming 2 December 16th 05 09:40 PM
macro to run a separate macro dependent on value in cell scottwilsonx[_13_] Excel Programming 3 July 26th 04 02:30 PM
how to make macro not dependent on datafile which has date as its part. haisat Excel Programming 2 October 18th 03 12:14 PM


All times are GMT +1. The time now is 11:33 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"