Vba Next Business Day Including holiday - vba

I want to have a function to return the next business day of the date that is sent in as parameter.
Weekends and Canadian holidays should be included.
What is the easiest way to do this ? Is it possible to do this without setting a reference to a range of cells for the holidays' dates?

Unless you provide a list of holidays, you won't be able to calculate the next working date. Remember the rules for Easter involve the phase of the moon! So this is not a simple task.
That is before you start to consider which different countries. Even if you restrict yourself to the US holidays, it takes a fair bit of calculations just to get the 3rd Thursday in November etc
Here is a demonstration of how you could calculate the next working day.
I wouldn't hold just the holidays but every date in the calendar. Note for Easter I have fudged the holiday column to get the correct next working day answer.
You could miss a few steps in the calculation, I left them in for clarity.

Use the =WORKDAY() function. You'll have to specify a range of holidays as the third parameter.

Related

SQL Day Count Calculation between two dates using two tables T1 = Days, T2 = Week Ending Date

Picture
I would like to create a query that takes the Week Ending Date (typically a Friday) and calculate the number of Working Days in each Week.
I have a choice of either every day possible (All Days), or just the Working Days. For example, some weeks would be shorter than other due to designated Holidays. I coded each day as either 1 = Working Day, 2 = Sat or Sunday and 3 = Holiday.
Which would be the easier solution? Go exclusively the Working Days off the Working Days Qry against the Week Ending Table? Or use the All Days Qry against the Week Ending Table? Tried looking up as much as possible on this very helpful site, but couldn't quite find a match to this. Thanks
Tried looking up a lot here, but couldn't find an exact match to my problem. Mainly a Design View guy here, with limited SQL experience.
Which would be the easier solution?
I'm not sure, but a simpler method could be to record the holidays separately, and then calculate the difference in workdays between any two dates.
For this, you can use my function DateDiffWorkdays.
For extended date handling and calculation, see my library at GitHub: VBA.Date.

Google Sheets monthly sum code optimization

Good day everybody, we are making a spreadsheets for incoming cash monitoring, and I feel like the method I used to achieve the monthly sum is possibly the worst.
So I was wondering if some of you guys have a shorter solution
=SUM(FILTER('Dashboard'!D2:D;'Dashboard'!E2:E="Incoming";'Dashboard'!C2:C>=DATE(text(today()-text(today();"dd");"yyyy");(text(today()-text(today();"dd");"mm"));(text(today()-text(today();"dd");"dd")));'Dashboard'!C2:C<=DATE(text(today();"yyyy");(text(today();"mm"));(text(today();"dd")))))
So since this looks like a cluster**** of code, i will try to annotate it:
=SUM(FILTER('Dashboard'!D2:D;'Dashboard'!E2:E="Incoming"
Sort by only the incoming cash and not outgoing
;Dashboard'!C2:C>=DATE(text(today()-text(today();"dd");"yyyy");(text(today()-text(today();"dd");"mm"));(text(today()-text(today();"dd");"dd")));'Dashboard'!C2:C<=DATE(text(today();"yyyy");(text(today();"mm"));(text(today();"dd")))
The range is from 1'st day of the month to todays date.
Method: Get todays date, and subtract todays date, to get the first day of the month.
Which isn't even a true monthly sum, rather than up to current day sum.
I'm really sorry but due to company policy I cant link the file itself, but the sheet is rather simple
The columns are:
Date, Sum, "Incoming/Outgoing", "Cash/Credit"
I also have a weekly sum, but I feel like that formula is somewhat decent
=query(filter('Dashboard'!C2:D;'Dashboard'!E2:E="Incoming";weeknum('Dashboard'!C2:C;1)=weeknum(today();1));"Select Sum (Col2) label Sum(Col2)''";-1)
There's no need to format the date to 'yyyy-mm-dd'. You can use EndOfMONTH to get the last day of last month.
=SUM(FILTER('Dashboard'!D2:D;'Dashboard'!E2:E="Incoming";'Dashboard'!C2:C>EOMONTH(TODAY(),-1);'Dashboard'!C2:C<=TODAY())

Find All 'Rental' that dont have 'Bookings' within a given DateRange

Proposal
What im trying to do, is allow people to input a date range, and find all 'Rentals' places that have open days between the provided date range from the user.
My Rentals have a has_many association with "bookings" which contain a start and end date.
What would be the best possible way to get every rental article that has a day that is open within that range.
Issue
I can find all the Rentals that have Bookings that dont overlap with the given time, but that doesnt account for each day. So if there was a booking from 7 Sept to 10 Sept, and the User entered the range 5 Sept to 9 Sept, that record would simply not be included, but in theory there is the 5th-7th open.
My Possible Solution
Im still thinking this problem through, so i have not started coding. But what i was going to do, is find all the Rentals that DONT have bookings that overlap with the provided Date Range, store those in a variable(because these are correct results, because it means not a single day overlaps).
Then i will find all the Rentals that have bookings that DO overlap with the provided Date Range. I will then loop through those bookings, and within that, loop through the Range of Dates provided by the user(DateRange.Each) and check if each single day is within that bookings date ranges, as soon as a single day results in a false value(meaning at least one day doesnt overlap) it will end the loop and add the associated "rental" article to the articles collected early with no overlaps at all.
Concerns
The only issue i have with this solution, is i feel it might not be very optimized or run well, im not sure. At the worst case, i would have hundreds of "rentals' and im not sure how many different "Bookings". I dont think something like this could be done in a single SQL query could it ?
Sample data
Rentals Model
name: string
has_many Bookings
Bookings Model
rental_id: integer
start_date: Date
end_date: Date
The user inputs a DateRange, i want all Rentals that have open days(none taken by a 'Booking' in the range provided.
EDIT
I have been thinking, and i think the best solution is using postgres db, which allows for arrays. Ill then store all the dates of the booking, in the array, instead of storing the range.
Postgres supports checking if all the values in an given array is present in an array in the DB. So i could check if the array of 'wanted days' is 'contained' by a DB array, if it is, it means none of the days are free.

How to Extend the End Date in a SQL Calendar Table?

I have a report that, according to users, started miscalculating dates in one field in November 2015. After some digging around, I found that one of the tables the field referenced seemed to have an end date on 2015-10-31.
The "D" field seems to represent the day of the week, with Sunday being day 1 and Saturday being 7.
Is there a way to extend the calendar so that it ends further into the future, for example 2049-12-31?
Our calendar table, for a variety of reasons, goes the the end of the current year. We have written a query that adds a new year to this table. This query takes care of most of the fields in that table. It does not touch the holiday field. That is updated manually through a web page.
We send ourselves reminders. Starting in March, we send monthly reminders that we should think about adding another year. After ensuring that the database segment has space, and that none of the definitions, such as fiscal periods, have changed, we run the query that adds a year.
Later in the year we start mailing ourselves reminders about the holidays. Then we check to see if HR has declared them, and if so, update the records accordingly.
This meets our business requirements. Yours will be different of course.

Calculating holiday dates

I have to create "holiday" table and then create php script so I could show it on my site.
Holidays can be specific, like 15.05.2012 - 15-th of the may.
And non-specific: First(or second, third) sunday of july
Is there any way to create calculated column, so this phrase "First(or second, third) sunday of july", could turn into x.07.2012.
Use a calendar table. There is no magic code built into SQL Server that knows when Easter is. This article shows the basic premise - you fill up a table with all the dates from year x to year y, then you update a column called IsHoliday for the dates that are holidays based on specific logic (easiest to do this once, in a loop, then all your code later can refer to the calculated bit):
ASP Faq reference. The current link no longer works, this is the archive.org cached version of the page
The link in the answer now takes you to a bogus page that wants to load a virus. Just heads up.
http://codeinet.blogspot.com/2006/08/auxiliary-calendar-table-for-sql.html
This seems to be a working version.