Solved

Access VBA - Have the Previous Month display on an Excel report

Posted on 2004-08-24
5
2,175 Views
Last Modified: 2012-06-27
I need the current month - 1 to display in Excel using VBA.

I tried this in my template
Month(today())-1 and it gives what looks like a month (ie...for current it's 7) but when I change the format to mmmm then it displays January.  Basically it looks like it's having the month be the day.  Anyway, can someone help me please please please?  I don't care if it's in the excel template or in my vba code.  Either one will do.   Thanks - you guys rock.

Julia
0
Comment
Question by:artgal
  • 2
  • 2
5 Comments
 
LVL 12

Expert Comment

by:fulscher
ID: 11884069
Month(today())-1 returns a number and not a date. If you format this number as mmmm, Excel assumes that it's January 7, 1980 and therefore displays "January".

Here's an Excel formula that will do it:

=DATE(IF(MONTH(NOW())=1,YEAR(NOW())-1,YEAR(NOW())),IF(MONTH(NOW())=1,12,MONTH(NOW())-1),1)

And here's a bit of VBA code to do it:

Public Function LastMonth() As Date
    Dim D As Date
    D = Now
   
    LastMonth = DateSerial(IIf(Month(D) = 1, Year(D) - 1, Year(D)), IIf(Month(D) = 1, 12, Month(D) - 1), 1)
End Function

Hope this helps
Jan
0
 
LVL 13

Expert Comment

by:lucas911
ID: 11884103
You can try the format function combined with DateAdd:

Format(DateAdd("m", -1, Date), "mmmm")

That will give you the month - 1

Good luck
0
 
LVL 12

Expert Comment

by:fulscher
ID: 11884314
lucas911

It's way more elegant than my code, but... what would the function do if you try this on March 31st? (just wondering...)

Jan
0
 
LVL 4

Accepted Solution

by:
naivad earned 250 total points
ID: 11886858
It works because it is subtracting a month, not a day...

Format(DateAdd("m", -1, #3/31/2003#), "d") = February 28th

If the day is greater than the last day of the month, it just displays the last day of the month.
0
 
LVL 4

Expert Comment

by:naivad
ID: 12048960
I think you should have given lucas the points on this, I was just explaining the answer.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In today’s complex data management environments, it is not unusual for UNIX servers to be dedicated to a particular department, purpose, or database.  As a result, a SAS® data analyst often works with multiple servers, each with its own data storage…
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now