Get last day of previous quarter - vba

DateAdd("m", -(Month(Date) - 1) Mod 3 - 1, Date)
This gives me 6/28/2015 while I need 20150630 . I can work on the format part but don't know how to get the last day of previous quarter instead of today's date of last quarter.

This should work for VBA. It just determines the first day of the current quarter and then subtracts one day.
Debug.Print DateAdd("q", DatePart("q", Date) - 1, "1/1/" & Year(Date)) - 1

Related

Date Diff - Parse Today's Date to capture the end of the fiscal year

Trying to come up with a way to use today's date to return a value I can use in a DateDiff function.
DateDiff (Month, Sales_Date, the end of our fiscal year using today's date)
Our fiscal year starts 4/1 and goes through 3/31
Any date (from getdate()) on or before 3/31 I would use the '3/31/' + the value of the getdate's year.
Any date (from getdate()) on or after 4/1 I would use '3/31/' + the value of the getdate's year + 1.
The end result will give me the number of months from the date of the original sale date to the end of the current fiscal year
Example
Today's date is 6/18/20, so the last parameter in the above DateDif formula would be 3/31/2021
I will be using the column in the below code:
Select g.sales_month,g.fiscal_year as FY, g.Sales_Type,v.region, sum(g.FY_Total) as FY_Total
from sales_FY g JOIN dbo.dimsummary AS v ON g.sales_id = v.sales_id
group by g.sales_Month, g.fiscal_year, v.Region, g.Sales_Type
Example:
Picture of the end result using Excel format
Subtract 3 months and extract the month. That gives you the month of this month in the fiscal year. Then subtract this value from 11 or 12 (I'm not exactly sure which is better for "number of months left"):
select 11 - month(dateadd(month, -3, getdate())

How to decipher complex DATEADD function from MS Access

I have inherited a query from an old MS Access DB and cannot for the life of me figure out what was trying to be done in this date parameter function. I normally only use SQL and this seems a bit different. Can any one assist in describing what this logic is doing?
use pdx_sap_user
go
select po_number,
po_issue_date
from vw_po_header
where po_issue_date > getDate() And PO_issue_date < DateAdd("d",-1,DateAdd("m",8,DateAdd("d",-(Day(getDate())-1),getDate())))
You can de-obfuscate it a lot by using DateSerial:
where
po_issue_date > getDate() And
po_issue_date < DateSerial(Year(getDate()), Month(getDate()) + 8, 0)
First: there is no getDate() function in Access. Probably it should be Date() which returns the current date.
Now starting from the inner expression:
Day(Date()) returns the current day as an integer 1-31.
So in DateAdd("d", -(Day(Date())-1), Date()) from the current date are subtracted as many days as needed to return the 1st of the current month.
Then:
DateAdd("m", 8, DateAdd("d", -(Day(Date())-1), Date()))
adds 8 months to the the 1st of the current month returning the 1st of the month of the date after 8 months.
Finally:
DateAdd("d", -1,...)
subtracts 1 day from the date returned by the previous expression, returning the last day of the previous month of that date.
So if you run today 13-Sep-2019 this code, the result will be:
30-Apr-2020
because this is the last day of the previous month after 8 months.
I think the following:
Take the current date
Substract the current day of month -1 to get the first day of current month
Add 8 month to this
Substract 1 day to get the last day of the previous month
So it calculates some deadline in approx 8 months.
But I wonder how a PO issue date can be in the future...

VBA convert week and day to date

I have a table with week number and day of week.
Example:
J2 = 18
G2 = Monday
How can I convert this to 2018-04-30?
I can find lots of threads for converting the other way around, meaning date to week. But I can't find anything on week + weekday to date.
Anyone know of a method?
The (optional) second argument of the WEEKDAY function determines what the first day of the week is. The two most common choices are:
1: Sunday (1) to Saturday (7)
or
2: Monday (1) to Sunday (7)
But, you can start on Wednesday, Friday, etc if you want. Pick an option now.
So, start with 1st January (of whichever unspecified year you're working with), and subtract the weekday of 1st January, according to whichever start-day you picked. This gives you a baseline for Week 1. To get to the start of Week n, you just need to add 7*(n-1) days.
Finally, you need to add the weekday back on. I'm going to recommend using MATCH on an array. Your array will be your weekdays, in order, surrounded by curly brackets. So, for Sunday-to-Saturday, you would use
MATCH(G2,{"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"},0)
to get a number from 1 ("Sunday") to 7 ("Saturday"), or #NA if cell G2 does not contain a day-name.
Stick it all together, and you get something like this:
=DATE(YEAR(NOW()),1,1) - WEEKDAY(DATE(YEAR(NOW()),1,1),1) + 7*(J2-1) + MATCH(G2,{"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"},0)
Try this, necessary comments in code:
Option Explicit
Sub GetDate()
Dim year As Date, day As Long
'convert day in G2 to integer
Select Case Range("G2").Value
Case "Monday"
day = 1
Case "Tuesday"
day = 2
'rest of cases
End Select
'declare what year you want
year = "01-01-2018"
'move from first week to the week specified in J2
year = DateAdd("d", (Range("J2").Value - 1) * 7, year)
'move back to first day of the week
Do While DatePart("w", year, vbMonday) <> 1
year = DateAdd("w", -1, year)
Loop
'find date of particular day
Do While DatePart("w", year, vbMonday) <> day
year = DateAdd("w", 1, year)
Loop
End Sub
The determination depends critically on your definition of "week number".
If you are using the ISO-8601 definition, where a Week starts on Monday and week 1 contains the first Thursday of the year, you can use this worksheet formula:
=DATE(YEAR(TODAY()),1,-2)-WEEKDAY(DATE(YEAR(TODAY()),1,7),14)+$J$2*7+MATCH($G$2&"*",{"Monday";"Tuesday";"Wednesday";"Thursday";"Friday";"Saturday";"Sunday"},0)-1
you may need to change the commas to semicolons depending on your regional settings
To explain:
First, find the last Monday of the previous year:
DATE(YEAR(TODAY()),1,-2)-WEEKDAY(DATE(YEAR(TODAY()),1,7),14)
Then add seven(7) times the number of desired weeks:
+$J$2*7
Then add the number of days from Monday to the desired day for that week:
+MATCH($G$2&"*",{"Monday";"Tuesday";"Wednesday";"Thursday";"Friday";"Saturday";"Sunday"},0)-1

Month Ending Date VBA

I need help writing one line of code in VBA to return the Month-Ending date. The logic for Month-Ending date works like this...
If the last day of the month ends on...
Sunday: Then last day of that month is that previous Saturday. (Yesterday)
Monday: Then last day of that month is that previous Saturday. (2 days ago)
Tuesday: Then last day of that month is that previous Saturday. (3 days ago)
Wednesday:Then last day of that month is the upcoming Saturday (3 days in the future)
Thursday: Then last day of that month is the upcoming Saturday (2 days in future)
Friday: Then last day of that month is the upcoming Saturday (1 day in future)
My current code is below. The formatting of the Month-Ending date is as follows. 2016-07-02
Sub Macro1()
With ActiveWorkbook.Connections("ABC Query").ODBCConnection
.BackgroundQuery = True
.CommandText = Array( _
"exec [dbo].[getBSC_Monthly] #MonthEndDate = **where I need the line of code**")
The Weekday() function will tell you what the current day of the week is (Sun=1, Mon=2, etc.). So, if Weekday() < 4, then you want the date Weekday() days ago. If WeekDay() >= 3, then you want 7 - Weekday() days in the future.
Function MonthEnd(d)
Dim actualmonthend, dow, ans
actualmonthend = DateSerial(Year(d), Month(d) + 1, 1) - 1
dow = Weekday(actualmonthend)
If (dow < 4) Then
ans = actualmonthend - dow
Else
ans = actualmonthend + (7 - dow)
End If
MonthEnd = ans
End Function
if you really just want an expression this would work:
DateSerial(Year(d), Month(d) + 1, 1) - 1 - Weekday(DateSerial(Year(d), Month(d) + 1, 1) - 1) + (7 * Abs(Weekday(DateSerial(Year(d), Month(d) + 1, 1) - 1) >= 4))

Calculate the last day of the next month

How to calculate the last day of the next month using vb.net?
try yo use DaysInMonth Function which returns the number of days in the required month.
Example
System.DateTime.DaysInMonth(2016,May)
This will return (31), so you can get the date format in addition to this number to get last day of this month.
An other way is to get the first day of the month after and substract 1 day.
Dim d As DateTime
d = DateTime.Now.AddMonths(2)
d = New DateTime(d.Year, d.Month, 1).AddDays(-1)
If you after the actual date....
You could use DateSerial(Year, Month, 0) this will return the last day of the month prior to the month entered.
Entering DateSerial(2016, 07, 0) will return "30/06/2016"
To get the last day of next month do DateSerial(Now.Year, Now.Month + 2, 0)
This works fine for February and also the year end as well (DateSerial(2016, 3, 0) returns "29/02/2016", DateSerial(2016, 11 + 2, 0) returns "31/12/2016")
Dim N = DateTime.Now.AddMonths(1)
Dim D = New DateTime(N.Year, N.Month, DateTime.DaysInMonth(N.Year, N.Month))
D will have the last day of next month.