I am trying to build a loop which prints a list with the 15th day and the last day of each month. The starting date of the list will depend on the current day (num_day), whereas the length of such list will depend on a given N number.
For example, if today is 30/07/2021 (dd/mm/yyyy format), and the N = 5 the list should be the following:
- 31/07/2021, 15/08/2021, 31/08/2021, 15/09/2021, 30/09/2021
My current VBA code is the following:
Sub print_dates()
N = 9
For i = 0 To N - 1
curr_day = DateAdd("m", i, Date)
num_day = Format(Date, "dd")
If num_day <= 15 Then
If i Mod 2 = 0 Then
print_day = "15/" & Format(curr_day, "mm") & "/" & Format(curr_day, "yyyy")
Else
print_day = DateSerial(Year(curr_day), Month(curr_day), 0)
End If
Else
If i Mod 2 = 0 Then
print_day = DateSerial(Year(curr_day), Month(curr_day), 0)
Else
print_day = "15/" & Format(curr_day, "mm") & "/" & Format(curr_day, "yyyy")
End If
End If
Debug.Print print_day
Next i
End Sub
With my current code the result of the list is:
- 30/06/2021
- 15/08/2021
- 31/08/2021
- 15/10/2021
- 31/10/2021
- 15/12/2021
- 31/12/2021
- 15/02/2022
- 28/02/2022
The months with odd numbers (7, 9, 11, etc.) are being skipped by the code. Also, the list starts with last month's last day. Are there any suggestions on how to reach the desired result?
Thanks a lot in advance.