Help with an Array between two given dates

Desmondo

Board Regular
Joined
Feb 27, 2013
Messages
70
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.

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:

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.

Forum statistics

Threads
1,223,731
Messages
6,174,178
Members
452,550
Latest member
southernsquid2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top