How to determine week number within calendar quarter period VB6 - vba

Having some trouble locating a function to determine the week number of a quarter period in a standard date based (i.e. Day 1 = 2017-01-01) calendar table.
My table has the following information stored :
Quarter Number, Beginning Date, End Date, Number of Days;
As an example, for the first quarter of calendar year, the result for Week 1 would be 1, Week 14 would be 1, going through each quarter until the final week of quarter 4.
Any ideas please help?

I don't believe there is a built-in function for this. If I understand what you're asking, this is a trivial function to write. You don't include any code so I'm just giving you the most basic example. You want to do something similar to this.
Public Function GetWeekInQuarter(ByVal WeekNumber As Integer) As Integer
Const intWeeksInQuarter = 13
Dim intResult As Integer
intResult = WeekNumber Mod intWeeksInQuarter
'if intResult <> intWeeksInQuarter = 0 then this is the 13th week
GetWeekInQuarter = IIf(intResult <> intWeeksInQuarter, intResult, intWeeksInQuarter)
End Function

Related

How can I find the time number of days between a given date, and the given date's first day of the year in VBA?

For example, the inputted date is 2001/01/21 the output must be 21 because 21 days passed since the inputted date's first day of the year.
Transform the date to a value and do the same for the "new" date? Do the same in Dev.mode newCelldate.Value - yearStartdate.Value?
Copy and paste the following to your VB Editor. Then, run the "TestNumberOfDays" procedure.
Function FirstDayOfTheGivenYear(pDate As Date)
'this will return first day of the given year
FirstDayOfTheGivenYear = 1 * (CDate(pDate) - DatePart("y", CDate(pDate) - 1))
End Function
Function YTDDays(pDate As Date) As Long
'this will find the day differences between two dates
YTDDays = DateDiff("d", FirstDayOfTheGivenYear(pDate), pDate) + 1
End Function
Sub TestNumberOfDays()
'change the date according to your need
MsgBox (YTDDays("2001/01/30"))
End Sub
Like this:
InputDate = #2001/01/21#
OrdinalDay = DatePart("y", InputDate)

Day360 in vb.net

I want to use the Days360 function in VB.Net. I need to know the difference in days between two dates assuming 360 days in a year (not 365 days the DateDiff function uses).
For example DateDiff(DateInterval.Day,"16/10/2015", "04/02/2016") = 111 days, but Days360 should return 109 days.
Days360 function in Excel calculates the days between two dates using a fictional calendar that has 30 days in each month. This method is used for some financial purposes.
You can write a function to do the same calculation.
[Edit]
Excel supports two versions of the calculation: one common in the US (this is the default) and the other common in Europe (see the documentation of the DAYS360 function for details).
The code I originally posted implemented the European version. I have updated it to support both versions. Thanks to Nikhil Vartak for pointing this out.
Function Days360(startDate As DateTime, endDate As DateTime, euMethod As Boolean) As Integer
Dim months As Integer = (endDate.Year - startDate.Year) * 12 + endDate.Month - startDate.Month
If euMethod Then
'Use European method (start or end dates after the 30th of the month are changed to 30th)
Return months * 30 + Math.Min(30, endDate.Day) - Math.Min(30, startDate.Day)
Else 'Use US method
'If the start date is the last day of the month, change it to the 30th
Dim startDay As Integer = startDate.Day
startDay = If(startDate.Day >= DateTime.DaysInMonth(startDate.Year, startDate.Month), 30, startDate.Day)
'If end date is last of the month, change it to the 30th
Dim endDay As Integer = endDate.Day
endDay = If(endDate.Day >= DateTime.DaysInMonth(endDate.Year, endDate.Month), 30, endDate.Day)
'If end date is last of the month and start date is before 30th, change end date to 1st of the next month
If endDate.Day >= DateTime.DaysInMonth(endDate.Year, endDate.Month) And startDay < 30 Then
endDay = 1
months += 1
End If
Return months * 30 + endDay - startday
End If
End Function

Generate 29 days of February in .NET

I am trying to populate a dropdown with No.of days generated by the system by the following code
Sub LoadDay()
Try
ddlDay.Items.Clear()
For i As Integer = 1 To System.DateTime.DaysInMonth(DateTime.Today.Year, ddlMonth.SelectedValue)
ddlDay.Items.Add(New ListItem(i.ToString(), i.ToString("00")))
Next
ddlDay.Items.Insert(0, New ListItem("--Select--", -999))
Catch ex As Exception
End Try
End Sub
Problem is, for the month of February it shows only 28 days because the following code generates Days on the basis of current year and this year isn't a leap year
System.DateTime.DaysInMonth(DateTime.Today.Year, ddlMonth.SelectedValue)
How can I fix it to always show 29 days for the month of Februaray?
You can use DayInMonth in DateTime
Dim dayinMonth As String
dayinMonth = System.DateTime.DaysInMonth(2012, 2)
This allow you to know the day in month for 2015 February. Parameter inside stand for year and month
I'd be interested in knowing what your requirment is that you have to be able to select invalid days, but, if you don't want your days to change based on the year, just create your own array of days. This leaves your intent a bit more clear than putting an arbitrary year in the DaysInMonth function.
Dim Days = {31,28,31,30,31,30,31,31,30,31,30,31}
ddlDay.Items.Clear()
For i As Integer = 1 To Days(ddlMonth.SelectedValue-1)
ddlDay.Items.Add(New ListItem(i.ToString(), i.ToString("00")))
Next
ddlDay.Items.Insert(0, New ListItem("--Select--", -999))

VBA Set a specific date to be the first week of the year

I want to set a specific date (October 06 , 2014) to be the 1st Week of the year. So when I use the =weeknum() formulaR1C1 it will return the week number.
I'm currently working on a worksheet that gets updated daily so the week number will be updated every week only. In column ("D") indicates the week number. and column ("B") indicates the daily date.
If i use the =weeknum() formula on it returns the value of 41, but it needs to be week number 1.
How about creating your personalized function?
It just consists of counting how many days there are between the date you insert (myDate) and the date which is considered to be the first day of the year (first_day), dividing this number by 7, taking its integer and adding up 1. It will always give you the right one.
Public Function special_weeknum(ByVal myDate As Date) As Integer
Dim first_day As Date: first_day = #10/6/2014#
Dim myWeek As Integer: myWeek = Int((myDate - first_day) / 7) + 1
special_weeknum = myWeek
End Function
Please note that this approach would allow you also to pass the first day as a user input:
Public Function special_weeknum(ByVal myDate As Date, ByVal first_day As Date)
Dim myWeek As Integer: myWeek = Int((myDate - first_day) / 7) + 1
special_weeknum = myWeek
End Function
so that you will always be able to decide which is the day you must consider as first day of the year.

In VB, calculating the 5th working day of every month

In VB, what coding could I use to calculate the 5th working day of every month? And if the 5th day is a holiday to go up one day.
You would need a list of holiday dates with which to compare. You would need to build and store that list separately. You did not say what variant of VB (VBA? VB.NET?) but in VB.NET you could do:
Dim datevalue As DateTime = New DateTime(DateTime.Year, DateTime.Month, 1)
Dim dayIsFound As Boolean = False
Dim workDays As Integer
workDays = 1
While Not dayIsFound
If ( dateValue.DayOfWeek <> DayOfWeek.Saturday _
And dateValue.DayOfWeek <> DayOfWeek.Sunday _
And Not HolidayList.Contains( dateValue ) _
workDays = workDays + 1
End If
If index >= 5 Then
dayIsFound = True
Else
dateValue = dateValue.AddDays(1)
End If
End While
Technically, it is possible to build an algorithm that determines the major holidays based on Federal guidelines (in the US) but it is complicated and may not conform to the holidays of the company to whom you are building this component.
The problem has 2 main components - first you need a list of public holidays, and second you need to count days of the week which are working days in your locale.
You can hard code your holidays for a quick solution, and if you want this code to keep working without modification you will need to Google for some algorithms / functions to calculate your holidays. Here is a link to some sample calculation functions for holidays, including Easter. http://www.cpearson.com/EXCEL/holidays.htm
Your main function can use the VB functions Weekday and WeekdayName (if needed) in conjunction with your list of public holidays to count all days that fall on a normal working day which isn't in your list of public holidays.
Use Weekday function for Saturday and Sunday. Holidays are dependent on the location and country and you need to implement your own function for that.
You need to count each valid working day until you get to the fifth one. E.g.
index = 0
Do
If dateValue.DayOfWeek <> DayOfWeek.Saturday _
AndAlso dateValue.DayOfWeek <> DayOfWeek.Sunday _
AndAlso Not HolidayList.Contains( dateValue ) Then
index = index + 1
If index >= 5 Then _
Exit Do
End If
dateValue = dateValue.AddDays(1)
Loop