Increment a date by a number of days, months or ye

2019-09-21 14:46发布

I have a date which I will need to increment the date by a day, month or year from the actual date. This is based of a list of fields below and is different per the input. Also each resultant date has to be a business day i.e not a weekend day or a bank holiday. I will forgo the Bank Holiday requirement now.

I am using the formula =DATE(YEAR($A$1),MONTH($A$1),DAY($A$1)) and hard coding the increment value in each row based of the value in B4 below. Also once we get past SP in column A4 the formula changes to reference the date created by using the value at the SP value.

So if we put the results in to column E the formula will change to =DATE(YEAR($E$6),MONTH($E$6),DAY($E$6)) The date also needs to be in the format text(xx,"mm/dd/yyyy")

Any suggestions on how to create this in Excel of VIA a VBA solution macro would be appreciated

A1
=today()

A4 B4
ON  1 Day
TN  2 Day
SP  2 Day
SN  2 Day
1W  7 Day
2W  14 Day
3W  21 Day
1M  1 Month
2M  2 Month
3M  3 Month
4M  4 Month
5M  5 Month
6M  6 Month
7M  7 Month
8M  8 Month
9M  9 Month
10M 10 Month
11M 11 Month
1Y  1 Year
15M 15 Month
18M 18 Month
21M 21 Month
2Y  2 Year
3Y  3 Year
4Y  4 Year
5Y  5 Year
6Y  6 Year
7Y  7 Year
8Y  8 Year
9Y  9 Year
10Y 10 Year
15Y 15 Year
20Y 20 Year
25Y 25 Year
30Y 30 Year

1条回答
放荡不羁爱自由
2楼-- · 2019-09-21 15:16

I would create a user defined function, somthing like that

Function IncDate(ByVal dt As Date, ByVal add As Long, ByVal dmy As String) As Date

    Select Case UCase(dmy)
        Case "DAY"
            IncDate = DateAdd("d", add, dt)
        Case "MONTH"
                IncDate = DateAdd("m", add, dt)
        Case "YEAR"
                IncDate = DateAdd("yyyy", add, dt)
        Case Else
                IncDate = dt
    End Select
End Function

And then you could write in D

=IncDate(TODAY();B4;C4)

You then need to format the cell to your needs.

Result would look like that

enter image description here

查看更多
登录 后发表回答