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
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
This will change your date (one caviat) is that if it is 1/1/yyyy it goes to 31/12/yyyy but hopefully you can work round that.

Code:
Private Sub TextBox2_AfterUpdate()

Me.TextBox1.Value = DateAdd("yyyy", 1, Me.TextBox1.Value)
Me.TextBox1.Value = DateAdd("d", -1, Me.TextBox1.Value)

End Sub
 
Upvote 0
Hi James and thanks for your response.

Your code does the job very nicely until you have a delivery date of the 1st, at which point it gives an error.

I've just noticed lynxbci's response which appears to address that issue so I'll try it now. There won't be a problem with the 1st of January, we'll be closed!

Thanks very much
 
Upvote 0
I've just tried your method lynxbci and we have a different problem... if the date is the 1st of the month then it gives you a date of the 1st of the previous month, in the next year.

For example:

A delivery date of 01/06/2011 gives an expiry date of 01/05/2012

Dates are such a pain with Excel and VBA!!! Any ideas?
 
Upvote 0
Hi

Are you sure i have just tried 01/06/2011 and got 31/05/2012

Kev
 
Upvote 0
I've just tried your method lynxbci and we have a different problem... if the date is the 1st of the month then it gives you a date of the 1st of the previous month, in the next year.

For example:

A delivery date of 01/06/2011 gives an expiry date of 01/05/2012

Dates are such a pain with Excel and VBA!!! Any ideas?

Are you definatly using UK dates and not US dates? It looks like it thinks that 01/06 is 6th January.
 
Upvote 0
Hi, it may need tweaking slightly for your input method

This code allows you to enter a date in textbox2 and updates textbox1.
It adds a year to the date then subs a day from that new date

Code:
Private Sub TextBox2_AfterUpdate()

Me.TextBox1.Value = DateAdd("yyyy", 1, Me.TextBox2.Value)
Me.TextBox1.Value = DateAdd("d", -1, Me.TextBox1.Value)

End Sub
 
Upvote 0
I've just modified the code to the following:

Me.tbExpiry.Value = Format(DateAdd("yyyy", 1, Me.tbDeliveryDate.Value), "dd/mm/yyyy")
Me.tbExpiry.Value = Format(DateAdd("d", -1, Me.tbDeliveryDate.Value), "dd/mm/yyyy")

and it works (tbDeliveryDate is already formatted _afterupdate).

Thanks guys, it's so much more efficient than us typing it like we have done in the past!

Phil
 
Upvote 0

Forum statistics

Threads
1,224,587
Messages
6,179,733
Members
452,939
Latest member
WCrawford

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