Calculate If Date Is Every Other Friday And If So Do Something - vb.net

So I need to know how I can grab every other Friday starting with the date of a Friday, have the program check if the date is one of those dates and if so do X function. So far I found how to find the Friday after one Friday, but I need it to gather up the dates and save them then check against them.
Dim payday as Date= GetNext(DayOfWeek.Friday)
Function GetNext(ByVal d As DayOfWeek, Optional ByVal StartDate As Date = Nothing) As Date
If StartDate = DateTime.MinValue Then StartDate = Now
For p As Integer = 1 To 7
If StartDate.AddDays(p).DayOfWeek = d Then Return StartDate.AddDays(p)
Next
End Function
Then Here is the If Statement I am thinking of
If Date.Today = payday Then
'do this
End If
Rough sketch of what I was thinking.

Dim startDate As Date 'fill with an initial Friday
Function IsPayday(Optional dte as Date? = Nothing) As Boolean
dte = If(dte, Today)
Return DateDiff(DateInterval.Day, startDate, dte) Mod 14 = 0
End Function
and use it:
Dim yesterday = Today.AddDays(-1)
If IsPayday(yesterday) Then
'yesterday was a pay day
End If
If IsPayday() Then
'today is a pay day
End If

Related

Check if the day is between two given days using VBA in Excel

I've limited knowledge in coding with VBA in Excel and have run into a problem. I'm trying to write a code where it does something based on what day of the week it is.
I'm extracting the day of the week as string and then using a IF...ELSE bit. Code is attached below:
Sub Schedule()
Dim Today As String
Today = Format(Date, "dddd")
If Today >= "Monday" And Today <= "Thursday" Then
'code to do something
ElseIf Today = "Friday" Then
'code to do something
ElseIf Today >= "Saturday" And Today <= "Sunday" Then
'code to do something
End If
End Sub
Kindly advise.
A Select Case ... is fine for this - and do use constants, not magic numbers:
Sub Schedule()
Select Case Weekday(Date)
Case vbMonday To vbThursday
' Code to do something.
Case vbFriday
' Code to do something else.
Case vbSaturday, vbSunday
' Code to do some fun.
End Select
End Sub
Use WeekDay() to extract an integer representing the current day of the week:
Dim Today As VbDayOfWeek
Today = Weekday(Date)
If Today >= vbMonday And Today <= vbThursday Then
....
You cannot apply comparison operands (> or <) to strings so you will need to use the .WeekDay() function instead of formatting to string.
Here is the documentation: WeekDay please note the second optional parameter.
Here is the code:
Dim Today as Long
Today = WeekDay(Date)
If Today >= 2 And Today <= 5 Then
'do something
ElseIf Today = 6 Then
'do something
ElseIf Today = 7 Or Today = 1 Then
'do something
End If

Excel dates table intersection with VBA

I have one excel page and I store date type datas. This datas extra workdays and plus holidays in every year. Other table in this page has start and end date. I would like to show some interval the employees work days. I would like to use the VBA. How to solved this problem?
Use NETWORKDAYS.INTL()
=NETWORKDAYS.INTL(E2,F2,1,A2:A3)
This pictures the Excel table and my code:
Dim startDate As Date
Dim endDate As Date
Dim retValue As Integer
Dim days As Variant
Dim dates As Variant
days = Worksheets("Tényleges munkanapok").ListObjects("TáblázatKorrekciósNapok").DataBodyRange.Value
dates = Worksheets("Tényleges munkanapok").ListObjects("TáblázatSzámoló").DataBodyRange.Value
Dim i As Integer
Dim actualDate As Date
startDate = days(1, 1)
actualDate = dates(1, 1)
Dim CountVariant As Integer
CountVariant = UBound(days)
i = 0
Do While i <= CountVariant
If startDate < actualDate Then
i = i + 1
End If
Loop

Microsoft word VBA Expiration Date

Looking to change this Microsoft Word VBA Code so that the expiration date is always every Monday of the week, not a specific date:
Sub MyMacro()
ExpirationDate = #6/1/2013#
If Now() < ExpirationDate Then
'Rest of macro goes here
End if
End Sub
Any thoughts on how to do this would be great :)
If Weekday(Date) = 2 Then ... 'Monday
Public Function FindMonday(dt As Date) As Date
Do Until WeekdayName(Weekday(dt)) = "Monday"
dt = DateAdd("d", 1, dt)
Loop
FindMonday = dt
End Function
ExpirationDate = (Date + 7) - (Weekday(Date) - Weekday(vbMonday))
This formula will always return the date of next Monday, as specified by "+7"
You don't need to declare the variable Expirationdate.
If Date < (Date + 7) - (Weekday(Date) - Weekday(vbMonday)) Then
will do the job.
Note that Now returns a date/time value whereas Date returns a date integer. If time is of the essence you would have to add it in. Add 0.5 to the above formula to fix the expiration time on next Monday, 12 noon.
I'd usually use this function:
Public Function PreviousMonday(CurrentDate As Date) As Date
PreviousMonday = CurrentDate - Weekday(CurrentDate - 2)
End Function
You can then call it as:
PreviousMonday(Date()) - would return 24/07/2017 if entered today (28th).
PreviousMonday(CDATE("1 July 2017")) - would return 26/06/2017
PreviousMonday(42430) would return 29/02/2016 (42430 = 1st March 2016).

ISO week number in VBScript or VBA

How can I get the ISO week number of some date in VBScript or VBA?
First, note that:
It is important to report the week year along with the week number, as the date's year could be different.
Several Windows components contain a bug for some years' last Monday.
In my experience the simplest, clearest and most robust way to compute this is:
Sub WeekNum(someDate, isoWeekYear, isoWeekNumber, isoWeekDay)
Dim nearestThursday
isoWeekDay = WeekDay(someDate, vbMonday)
nearestThursday = DateAdd("d", 4 - Int(isoWeekDay), someDate)
isoWeekYear = Year(nearestThursday)
isoWeekNumber = Int((nearestThursday - DateSerial(isoWeekYear, 1, 1)) / 7) + 1
End Sub
This also returns the ISO day of the week, counting from 1 for Mondays.
Enter any date into A1 cell, then run following code...
Range("A2").FormulaR1C1 = "=INT((R1C-DATE(YEAR(R1C-WEEKDAY(R1C-1)+4),1,3)+WEEKDAY(DATE(YEAR(R1C-WEEKDAY(R1C-1)+4),1,3))+5)/7)"
You can get it via DatePart() VBA function:
Sub IsoWeek()
Dim isoWeekNum As Byte
Dim myDate As Date
myDate = DateValue("01-01-2022")
isoWeekNum = DatePart("ww", myDate, vbMonday, vbFirstFourDays)
If isoWeekNum > 52 Then ' Bug check (to avoid the bug with Mondays)
If Format(myDate + 7, "ww", vbMonday, vbFirstFourDays) = 2 Then isoWeekNum = 1
End If
Debug.Print isoWeekNum
End Sub

Find Quarter of DateTimePicker Selected Date

I have a DateTimePicker named dtpDateSelection. When I select a date I need to break the date down into two variables. I need Quarter to equal the quarter that the date is in. And I need Year to equal the year that the date is in. I thought I knew how to do this but I'm having trouble. Here is the code I've tried:
Dim Year As String = DatePart("yyyy", dtpDateSelection)
Dim Quarter As String = DatePart("q", dtpDateSelection)
And this is the error I get:
Additional information: Argument 'DateValue' cannot be converted to type 'Date'.
A simple solution:
Dim year As Integer = DateTimePicker.Value.Year
Dim quarter As Integer = ((DateTimePicker.Value.Month - 1) \ 3) + 1
So you have the DateTime value from the DateTimePicker.Value property.
Using that you can retrieve the year of the date, like this:
Dim year As Integer = DateTimePicker.Value.Year
For determining the quarter the date is within, try this:
Public Shared Function DetermineQuarter(dateTime As DateTime) As Integer
If dateTime.Month <= 3 Then
Return 1
End If
If dateTime.Month <= 6 Then
Return 2
End If
If dateTime.Month <= 9 Then
Return 3
End If
Return 4
End Function
Now you can get the quarter value, like this:
Dim quarter As Integer = DetermineQuarter(DateTimePicker.Value)
As others pointed out, a DateTime value has a .Month member. For the quarter, I have always used a simple Choose function
Dim quarter As Integer = Choose(DateTimePicker1.Value.Month, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4)
Beyond that, your two lines of code work, but you need to add the .Value to the end of dtpDateSelection. The DatePart function 2nd parameter has an Object type, so it will allow you to pass in a control (which is what you are doing), but the end result will be an error. The .Value changes it to the date selected in the picker.
Dim Year As String = DatePart("yyyy", dtpDateSelection.Value)
Dim Quarter As String = DatePart("q", dtpDateSelection.Value)