I have a created a little calculator which calculates various sums and deductions for a given month which works well. My problem is we often work with periods of up to 15 months. I want to be able to load in two dates for e.g. 21/03/2017-20/06/2017 and then on each submit it loads the next set of dates which it does just now until the end date is matched. We work a lot with past periods hence dates are going back a bit.
The calculator is done on a userform and has a calculate button and a submit button.
My biggest problem is how to treat months where the initial period starts on the 31st of the month or in leap years. There are two rules in my work place if start date is the 31st then the next period should also start on the last day of the next month until the period is finished.
So the 31/01/2017-27/02/2017
Then 28/02/2017 -31/03/2017
And 29/02/2016 to 30/03/2016 (Leap year) & next one starts on the 31/3/2016
Similarly if the period starts on the 29th or 30th of the month then each subsequent period must start on that date except in a leap year
29/03/2017 - 28/03/2017
30/03/2017 - 29/03/2017
30/1/2017-27/2/2017 or 28/2/17 in a leap year
And so on and so forth.
Leap years
29/1/2017 - 27/02/2017 or 28/2 in a leap year next
I previously was just using the date add function but its now two complicated for my small brain. And now think an array is likely the solution but haven't written any before. And not sure how i would go about it. This is roughly what i was doing before. Hopefully someone has overcame a challenge like this before.
The calculator is done on a userform and has a calculate button and a submit button.
My biggest problem is how to treat months where the initial period starts on the 31st of the month or in leap years. There are two rules in my work place if start date is the 31st then the next period should also start on the last day of the next month until the period is finished.
So the 31/01/2017-27/02/2017
Then 28/02/2017 -31/03/2017
And 29/02/2016 to 30/03/2016 (Leap year) & next one starts on the 31/3/2016
Similarly if the period starts on the 29th or 30th of the month then each subsequent period must start on that date except in a leap year
29/03/2017 - 28/03/2017
30/03/2017 - 29/03/2017
30/1/2017-27/2/2017 or 28/2/17 in a leap year
And so on and so forth.
Leap years
29/1/2017 - 27/02/2017 or 28/2 in a leap year next
I previously was just using the date add function but its now two complicated for my small brain. And now think an array is likely the solution but haven't written any before. And not sure how i would go about it. This is roughly what i was doing before. Hopefully someone has overcame a challenge like this before.
Code:
Sub testDate()Dim sDate As Date
Dim eDate As Date
If IsDate(Test.APS.Value) Then
sDate = Test.APS.Value
Else
MsgBox "This is not a valid date"
Test.APS.Value = vbNullString
Exit Sub
End If
If Day(sDate) = 31 Then
eDate = DateAdd("m", 1, sDate + 1) - 2 'Month with 31 Days
Test.EPS.Value = Format(eDate, "dd-mm-yyyy")
Else
eDate = DateAdd("m", 1, sDate) - 1
Test.EPS.Value = Format(eDate, "dd-mm-yyyy") ' Ap start Date ending the 31st MOnth
End If
End Sub
Last edited: