View Full Version : Userform in VBA and DATEDIFF function
cat_sneak
12-16-2013, 06:24 AM
Hi
I am new to the forum so apologies if I've posted this is the wrong place.
I am trying to create a userform with a calculated field based on data entered in 2 text boxes, the field I would like calculated is the date difference, I would like the value to be displayed in a user form and then posted, along with the rest of the data in a workbook to update a database.
Can anyone help?
wrightyrx7
12-16-2013, 06:40 AM
Something like this to get the difference.
Private Sub CommandButton1_Click()
TextBox3.Value = DateDiff("d", CDate(TextBox1.Value), CDate(TextBox2.Value))
End Sub
cat_sneak
12-16-2013, 08:05 AM
Will I have to create a command button to run the calculation then?
wrightyrx7
12-16-2013, 08:15 AM
No you could get it to calculate when you have finished entering data into Textbox2 if you wanted.
Private Sub TextBox2_AfterUpdate()
TextBox3.Value = DateDiff("d", CDate(TextBox1.Value), CDate(TextBox2.Value))
End Sub
cat_sneak
12-16-2013, 08:15 AM
I've got it, thank you! I've been trying to think up a way to do it for a while, I'm new to VBA and decided my firest project would be something fairly complicated!
wrightyrx7
12-16-2013, 08:28 AM
Haha no problem, thanks for the feedback.
Im not an expert with VBA either, my practice is TRYING to help people out in various forums :)
Aflatoon
12-17-2013, 02:31 AM
FYI, if you need the number of days between two dates, you can simply subtract one from the other.
wrightyrx7
12-17-2013, 02:42 AM
Might aswell make use of the Datediff function though, thats what its there for :P
Aflatoon
12-17-2013, 03:17 AM
True. After all, why write =A1+1 when you can write =DATE(YEAR(A1),MONTH(A1),DAY(A1)+1)
;)
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.