Dynamic Year

Dummy Excel

Well-known Member
Joined
Sep 21, 2005
Messages
1,004
Office Version
  1. 2019
  2. 2010
  3. 2007
Platform
  1. Windows
Hi All,
I'm going crazy trying to get this logic right...

I have a report that needs to be done monthly. The macro finds the previous month, opens it and copies the data into my new report for history. The problem im having is come Jan 11, its looking for the previous report as Dec 11.

Scenario for the predefined Values are:
Code:
YY	11
PreYY	10
CurMnth	Jan
PriorMnth	Nov
PreMnth	Dec
Customer	ABC
Business	XYZ

Rich (BB code):
    If YY = "11" And CurMnth = "Jan" And PreMnth = "Dec" Then
        Windows(Customer & " " & Business & " OF " & PriorMnth & " " & PreYY & ".xls").Activate
    Else
        Windows(Customer & " " & Business & " OF " & PriorMnth & " " & YY & ".xls").Activate
    End If

So my question is how can I make the "11" (in bold) dynamic so come 2012, I don't need to make any changes?

thanks in advance
Samuel
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
you can use the function

Year(Now()) to find out which year is the current one and use that to set the value of YY
 
Upvote 0
I think you're making it hard on yourself:
Code:
    Const Customer As String = "Ajax"
    Const Business As String = "Mousetraps"
    Dim tDateThis As Date
    Dim tDateLast As Date
    
    tDateThis = #1/1/2011#
    tDateLast = DateAdd("m", -1, tDateThis)
    
    MsgBox Customer & " " & Business & " OF " & Format(tDateLast, "mmm yy") & ".xls"
 
Upvote 0
Hi All - I was just about to add the below, as it will make it easier where I am coming from ...

Code:
    YY = format(DateSerial(Year(Now), Month(Now), Day(Now)), "yy")
    PreYY = format(DateSerial(Year(Now) - 1, Month(Now), Day(Now)), "yy")
    CurMnth = WorksheetFunction.Proper(format(DateSerial(Year(Now), Month(Now), Day(Now)), "Mmmm"))
    PriorMnth = WorksheetFunction.Proper(format(DateSerial(Year(Now), Month(Now) - 2, Day(Now)), "MMM"))
    PreMnth = WorksheetFunction.Proper(InputBox("Enter Previous Month as Mmm", "Month", "Jan"))
 
Upvote 0
You could do this without an if:
Code:
Dim PriorMnth As Date
PriorMnth = DateAdd("D", -1, "01/" & Month(Now) & "/" & Year(Now()))
Windows(Customer & " " & Business & " OF " & Format(PriorMnth, "Mmm") & " " & Format(PriorMnth, "YY") & ".xls").Activate

Edit: Hehe Dan's late to the party once again :p
 
Upvote 0
You don't need all those variables -- all you need is one date variable that you can format as you like as needed, and calculate the prior month from that.
 
Upvote 0
shg4421 - so how do I use my IF statement as when we are in Feb 11, the previous month will be Jan 11?

Hey Dan - As you can see im still learning :)
 
Upvote 0
You don't need an If statement:
Code:
    Const Customer As String = "Ajax"
    Const Business As String = "Mousetraps"

    Dim tDateThis As Date
    Dim tDateLast As Date
    
    tDateThis = #1/1/2011#
    tDateLast = DateAdd("m", -1, tDateThis)
    
    Workbooks(Customer & " " & Business & " OF " & Format(tDateLast, "mmm yy") & ".xls").Activate
 
Upvote 0
shg4421 - maybe ive missed something here...

your code works well if the month is Jan 11, although if the month is Feb 11, I still get Dec 10 when it should be Jan 11
 
Upvote 0

Forum statistics

Threads
1,224,527
Messages
6,179,351
Members
452,907
Latest member
Roland Deschain

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top