Countdown to a specific date & time in VBA - excel

time = DateSerial(2020, 10, 13)
I can use DateSerial and a Do-While Loop in VBA to make a countdown to 13th October 2020 midnight with the above code. How can I make it countdown to 10 am on 13th October 2020?
Thank you.

Add 10 hours using TimeSerial:
time = DateSerial(2020, 10, 13) + TimeSerial(10, 0, 0)

Related

How to get the Week of the month (1:6)

We can find different approaches to determining the week of the month, and even though there are many pages on 1:4 and/or 1:5, there is very few around 1:6 approach.
So to give you a bit of the context, I am working with a pivot table in Excel which gets its values from a Power Query source.
In Power Query, there is a function Date.WeekOfMonth which takes in the date and returns a number between 1 and 6.
In this definition, weeks start from Sunday and ends on Saturday.
So, for example, the first two days of October 2021 -i.e. Fri & Sat- fall in the 1st week of Oct, while the 3rd day of Oct 2021 starts the second week, and then the last day of October 2021,i.e. Oct 31, is the only day in the 6th week.
I had an automation task on hand in which I needed to pull data from the Power Query-generated pivot, so I had to implement a piece of code in VBA which calcs weeks the same.
Unfortunately, I couldn't find any prepared snippet, so after implementing I though it might worth sharing.
(Any comments and suggestions appreciated)
The DatePart function is perfect for this. Using DatePart with the interval set to "weeks" you can find the week of a given date. Then you subtract the number of weeks before the first day of that month (which sets the first day to week = 1).
Function WeekNumOfDate(D As Date) As Integer
WeekNumOfDate = DatePart("ww", D) - DatePart("ww", DateSerial(Year(D), Month(D), 1)) + 1
End Function
Here is a second version of the function that has the ability to set the FirstDayOfWeek argument:
Function WeekNumOfDate(D As Date, Optional FirstDayOfWeek As VbDayOfWeek = vbSunday) As Integer
WeekNumOfDate = DatePart("ww", D, FirstDayOfWeek) - DatePart("ww", DateSerial(Year(D), Month(D), 1), FirstDayOfWeek) + 1
End Function
As an example for using FirstDayOfWeek: With FirstDayOfWeek set to vbThursday, the date "Nov 5th, 2021" will return as Week 2, whereas it would by default be counted as Week 1. November 1st to 3rd of 2021 will be week 1, and then 4th to 10th will be week 2.
Its implementation in VBA is:
Function WeekOfMonth(My_Date As Date)
If Day(My_Date) > Day(My_Date - 1) And Weekday(My_Date) > Weekday(My_Date - 1) Then
WeekOfMonth = WeekOfMonth(My_Date - 1)
ElseIf Day(My_Date) > Day(My_Date - 1) And Weekday(My_Date) < Weekday(My_Date - 1) Then
WeekOfMonth = WeekOfMonth(My_Date - 1) + 1
Else
WeekOfMonth = 1
End If
End Function
Note that even though the above function is recursive, its time and space complexity is an expression of order N, which here cannot exceed 31.

Get the first Friday of the Quarter of Today's Date

I was trying to get the first Friday of the quarter of today's date or any given date. Let's say the date is 12/06/2020, which falls to the 2nd quarter of the year. I should be able to get the first Friday of the 2nd Quarter and that should be April 3rd.
I was only been able to get the quarter of a given date, which you can see in the code below. Been stuck here for a while. Thanks in advance.
quarter = Int((Month(Now) + 2) / 3)
Here's a function that takes a date and returns the first Friday of the quarter:
Function FirstFridayOfTheQuarter(MyDate As Date) As Date
Dim FirstDayOfTheQuarter As Date
FirstDayOfTheQuarter = DateSerial(Year(MyDate), Int((Month(MyDate) - 1) / 3) * 3 + 1, 1)
FirstFridayOfTheQuarter = DateAdd("d", (13 - Weekday(FirstDayOfTheQuarter)) Mod 7, FirstDayOfTheQuarter)
End Function
This function is taking advantage of the Weekday function that returns:
1 for a Sunday
2 for a Monday
3 for a Tuesday
4 for a Wednesday
5 for a Thursday
6 for a Friday
7 for a Saturday
In case you want a non VBA solution:
My formula is:
=CEILING(EOMONTH(DATE(YEAR(A1);ROUNDUP(MONTH(A1)/3;0)+(ROUNDUP(MONTH(A1)/3;0)-1)*2;1);-1)-5;7)+6

Excel 2016 calculate fiscal year

Fiscal Year starts July 1 - June 30, I need to calculate and display the number of Fiscal Years between 2 dates.
For example: 01/01/2018-12/31/2018. Result (YYYY-YYYY) should give 2 fiscal years:
2017-2018 (July 1, 2017 - June 30, 2018)
2018-2019 (July 1, 2018 - June 30, 2019)
Its easy to calculate if there are only 2 fiscal years between 2 dates but using =IF(StartDate<DATE(YEAR(StartDate), 7,1),CONCATENATE(YEAR(StartDate)-1, "-", YEAR(StartDate)), CONCATENATE(YEAR(StartDate), "-", YEAR(StartDate)+1)) or
=IF(EndDate<DATE(YEAR(EndDate), 7,1),CONCATENATE(YEAR(EndDate)-1, "-", YEAR(EndDate)), CONCATENATE(YEAR(EndDate), "-", YEAR(EndDate)+1))
But how do I capture if there are more than 2 fiscal years? For example: 01/01/2018 - 09/20/2019
I would prefer the fiscal year be entered in a table range and automatically insert a new row if there are more than 2 fiscal years. I'm looking for either formula or vba solution.
last time i had to do this was for a specific report where the dates are written in cells:
dim s as long, e as long
if month(cells(1,1).value) >= 7 AND day(cells(1,1).value) >= 1 Then
s = year(cells(1,1).value)+1
else
s = year(cells(1,1).value)
end if
if month(cells(2,1).value) >= 7 AND day(cells(1,1).value) >= 1 Then
e = year(cells(1,1).value)+1
else
e = year(cells(1,1).value)
end if
'FY range:
cells(3,1).value = "FY" & s & "-FY" & e
rehashed the code for you, untested

What if statements with dates

I would like the following
If q14 is between 1/7/18 and 31/7/18, T14, 0
Q14 =15/2/19
T14 4320
I would expect the answer to be 0
Any ideas how I correctly put this into a formula?
Thanks
Break it down slightly:
If Q14 >= 2018-07-01 And Q14 <= 2018-07-31 Then T14, Otherwise 0
To make a Date show as a date instead of text, you want to use the DATE function, which accepts the Year, Month and Day like so: DATE(2017, 7, 1)
The way that "And" is handled in Excel is slightly unusual: You have an AND function, and you pass all of your conditions as arguments:
AND(Q14>=DATE(2017, 7, 1), Q14<=DATE(2017,7,31))
Then you can add this to a normal IF function:
=IF(AND(Q14>=DATE(2017, 7, 1), Q14<=DATE(2017,7,31)), T14, 0)
(Extra note: Since a day runs from 00:00:00 to 23:59:59, you usually want to check if you are before the next day instead of on the current day, which would be Q14<DATE(2017, 8, 1))
You can just test if between using and
so if Q14 is Greater than (using the date formula) 1st July 2018 and Q14 is less than 31st July 2018.
If true return cell T14 else 0
=IF(Q14>=DATE(2018,7,1)&Q14<=DATE(2018,7,31),T14,0)

How do you determine Daylight Savings Time in VBA?

What function will let us know whether a date in VBA is in DST or not?
For non-current dates (DST 2007+):
First, you need a function to find the number of specific weekdays in a month:
Public Function NDow(Y As Integer, M As Integer, _
N As Integer, DOW As Integer) As Date
' Returns Date of Nth Day of the Week in Month
NDow = DateSerial(Y, M, (8 - Weekday(DateSerial(Y, M, 1), _
(DOW + 1) Mod 8)) + ((N - 1) * 7))
End Function
Then, you can check for the DST day versus the following function calls:
Fall: NDow(Year(newdate), 11, 1, 1)
Spring: NDow(Year(newdate), 3, 2, 1)
For the current date:
Call the Windows API function GetTimeZoneInformation,
and it will return an enum (integer) with the status.
I got the code for this from Chip Pearson's great Excel site.
Pearson's site
For anyone wondering how to account for daylight saving time in Europe (central europe time), I modified the script from Chip Pearson. The last sunday of March (2 AM to 3 AM) and October (3 AM to 2 AM) are the days when the hour switching occurs.
Following code is the click event of a button in Excel:
Dim dates As String
dates = "A1:A20"
For Each c In Worksheets("Sheet1").Range(dates).Cells
If (IsDateWithinDST(c.Value)) Then
c.Value = DateAdd("h", 1, c.Value)
End If
Next
The module containing the necessary methods are to be found here.
More info on DST in Europe.

Resources