[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

VB help

Posted on 2012-09-19
1
Medium Priority
?
410 Views
Last Modified: 2012-09-19
Private Sub CommandButton1_Click()
Dim dtFirst As Date
Dim dtLast As Date

        dtFirst = DateSerial(Year(Date), Month(Date), 1)
        dtLast = DateSerial(Year(Date), Month(Date) + 1, 0)
       
       Range("C2").Value = dtFirst + 6 - Weekday(dtFirst - 1, 3)
       
       Range("C3").Value = dtLast + 1 - Weekday(dtLast + 1 - 6)
End Sub

If the code above works out the first monday of each month (C2) & the last friday (C3)

What would be the code to work out the same for last month?

Thanks
0
Comment
Question by:Kiwi-123
1 Comment
 
LVL 13

Accepted Solution

by:
Shanan212 earned 2000 total points
ID: 38415276
dtFirst = DateSerial(Year(Date), Month(Date) - 1, 1)
        dtLast = DateSerial(Year(Date), Month(Date), 0)

^ those 2 lines would change as such

You may rename them to get last month
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
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…

834 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