PC Review


Reply
Thread Tools Rate Thread

Date dependent macro - run once only

 
 
Wes_A
Guest
Posts: n/a
 
      17th Mar 2010
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 With Quote
 
 
 
 
Peter T
Guest
Posts: n/a
 
      17th Mar 2010
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" <(E-Mail Removed)> wrote in message
news:7E5D1F58-C3EE-47F1-8807-(E-Mail Removed)...
> 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 With Quote
 
OssieMac
Guest
Posts: n/a
 
      17th Mar 2010
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


 
Reply With Quote
 
Dennis Tucker
Guest
Posts: n/a
 
      17th Mar 2010
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" <(E-Mail Removed)> wrote in message
news:7E5D1F58-C3EE-47F1-8807-(E-Mail Removed)...
> 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 With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Date-dependent calculation =?Utf-8?B?U3RldmUgTydEb25uZWxs?= Microsoft Excel Worksheet Functions 2 28th Mar 2007 02:50 AM
date dependent link Glenn Microsoft Frontpage 18 27th Feb 2005 05:18 PM
Dependent date field =?Utf-8?B?bWFsMTkyMA==?= Microsoft Access Forms 3 14th Feb 2005 05:53 AM
macro to run a separate macro dependent on value in cell scottwilsonx Microsoft Excel Programming 3 26th Jul 2004 02:30 PM
Re: how to make macro not dependent on datafile which has date as its part. Bob Phillips Microsoft Excel Programming 0 18th Oct 2003 12:14 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:16 AM.