View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.misc
Bob Phillips Bob Phillips is offline
external usenet poster
 
Posts: 10,593
Default I require code to run a macro dependant on the date

This will work by checking the last run date on a specified sheet, the code
allows you to change my settings, and if it is a change of month, it runs
the appropriate macro. It assumes that the macros are called macro_Jan,
macro_Feb, etc.

Private Sub Workbook_Open()
Const SH_DATA As String = "Sheet1" '<=== change to suit
Const WS_CELL As String = "H1" '<=== change to suit

If Val(Format(Date, "yyyymm")) _
Val(Format(Worksheets(SH_DATA).Range(WS_CELL).Valu e, "yyyymm"))
Then
Application.Run "macro_" &
Format(Worksheets(SH_DATA).Range(WS_CELL).Value, "mmm")
Worksheets(SH_DATA).Range(WS_CELL).Value = Date
End If

End Sub

'This is workbook event code.
'To input this code, right click on the Excel icon on the worksheet
'(or next to the File menu if you maximise your workbooks),
'select View Code from the menu, and paste the code

Thge m onth macros, macro_Jan etc., should be placed in a standard code
module.

You will also need to store last month's date in the data cell, else the
code never runs.

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"Marie Bayes" wrote in message
...
I need to run a macro over a year, I would like to set it up at the start

of
the year but set each part to run dependant on what month I'm in. I

always
run the report for data entered up to yesterday, ie, if it's the 1st of

Feb
will be running Jan's macro. Is there a way I can write into the code to
call the section just for that month and then at the end of that section
continue with the remainder of the macro? Thanks in advance for your

help.