Solved

Excel VBA:  Date range formula

Posted on 2014-10-15
5
307 Views
Last Modified: 2014-10-15
Hello Experts,

Is there a formula in excel that would provide a weekly range for the previous week (Monday to Sunday).  So example if I was to use the formula now, it will show
10-06-14 to 10-12-14


it should look the same as the above format.

Much appreciated it!
0
Comment
Question by:Maliki Hassani
5 Comments
 
LVL 12

Assisted Solution

by:jkaios
jkaios earned 167 total points
ID: 40383018
To calculate last week:

Dim dtBeg as date, dtEnd as date

dtBeg = DateAdd("ww", -1, Date - Weekday(Date) + 1)
dtEnd = DateAdd("ww", -1, Date - Weekday(Date) + 7)

msgbox dtBeg & " - " & dtEnd

Open in new window

0
 
LVL 12

Expert Comment

by:jkaios
ID: 40383025
After you've calculated last week's date range, you can then format it to the way you want:

msgbox "beg date: " & format(dtBeg, "MM-dd-yy") & vbcrLf
               "end date: " & format(dtEnd, "MM-dd-yy")
0
 
LVL 76

Assisted Solution

by:GrahamSkan
GrahamSkan earned 166 total points
ID: 40383057
@Maliki Hassani,
You ask for a formula but you have added VBA Basic programming as a topic. Be aware that VBA and formulae are different things. Formulae will normally give automatic updates in the sheet. VBA macros need to be run specifically and could take considerably more time to process,
0
 

Author Comment

by:Maliki Hassani
ID: 40383078
Great point. I will use a formula rather than a macro.
0
 
LVL 21

Accepted Solution

by:
Ejgil Hedegaard earned 167 total points
ID: 40383091
A formula could be like this

=TEXT(TODAY()-WEEKDAY(TODAY(),2)-6,"mm-dd-yy")&" to "&TEXT(TODAY()-WEEKDAY(TODAY(),2),"mm-dd-yy")

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

911 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

23 Experts available now in Live!

Get 1:1 Help Now