Increase the date in a textbox by 364 days

philfloyduk

Board Regular
Joined
Jan 6, 2011
Messages
82
Hi.

I have a textbox on a form that contains a delivery date, and another textbox for the user to enter an expiry date. I would like the form to automatically increase the delivery date by a year, less one day when i populate the form.

Thank you in advance for any help provided

Phil
 
I see you've got an answer but I'll post this anyway.
Code:
Private Sub TextBox2_AfterUpdate()
Dim dt1 As Date
Dim dt2 As Date
 
    If IsDate(TextBox2.Value) Then

        dt1 = DateValue(TextBox2.Value)
        
        dt2 = DateSerial(Year(dt1) + 1, Month(dt1), Day(dt1)) - 1
        
        TextBox1.Value = Format(dt2, "dd mmm yy")
        
    End If
    
End Sub
I always forget DateAdd exists in Excel, more used to using it in Access.:)

PS Isn't 31/12/2011 a year less a day after 1/1/2011?
 
Upvote 0

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Hi,

Your code needs changing, or it won't change the year, as it will amend the year and then amend the day based on the originmal year. Tweak line 2 of your code and voila all is good.

Code:
Me.tbexpiry.Value = Format(DateAdd("yyyy", 1, Me.tbdeliverydate.Value), "dd/mm/yyyy")
Me.tbexpiry.Value = Format(DateAdd("d", -1, Me.tbexpiry.Value), "dd/mm/yyyy")


------------------------------------------------------
Everyone is equal in the Double Dream House
 
Upvote 0
I'm going to push my luck now.... :eeek:

Some of our rentals have a set expiry date of 14th June. How would I get a 14/06/"next year" date?
 
Upvote 0
Only add a year, ie dont' subtract a day.

In my code I just commented out -1.
 
Upvote 0
2 seconds ahead of me. Nice



_____________________________________________
Everyone is equal in th Double Dream House
 
Upvote 0

Forum statistics

Threads
1,224,589
Messages
6,179,744
Members
452,940
Latest member
rootytrip

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