VBA - Userforms and Dates

Pacman52

Active Member
Joined
Jan 29, 2009
Messages
428
Office Version
  1. 365
Platform
  1. Windows
Hi all - its been years since I last had to create a userform or even touch VBA but I've been tasked with creating a workbook for my wife so that she can enter all her accounts data to make it easier for her to do her tax returns next year.

I've created an expense form - which so far works fine but I'm really struggling to work out an easy way for her to enter two dates on the form.
(Start Date - txtStart & End Date - txtEnd)

What I'd like to know is how do I code the two text boxes so that when data is entered it is automatically converted to the date format 'DD/MM/YYYY'
and once the dates have been entered I also need the number of days calculated with the result shown in another text box 'txtTotalDays' as a number i.e. if the start date is 10/10/2023 and the end date is 20/10/2023 the the total days would be 10. Once this form is submitted (by a button) the data populates the spreadsheet called 'Expenses'.

(On the spreadsheet I have formatted to relevent cells as a date).

I've been trying to work out how to do this now for two days and losing the will to live with google lol so would really appricate some advice please or a solution would be ideal !

Many thanks Paul
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
I named three Textboxes. sDate (TextBox1), eDate (TextBox2), and days (TextBox3). I used this code in the third (days) TextBox. I think it will do what you need. After start date (sDate) is entered, and end Date (eDate) is entered, and you click on (or Tab to) days box it will run.
VBA Code:
Private Sub days_Enter()
Dim sDate As Date, eDate As Date, days As Integer
sDate = Me.sDate.Value
eDate = Me.eDate.Value
days = eDate - sDate
Me.days = days
End Sub
 
Upvote 0
Solution
Many thanks for the reply Skyybot, I'll give that a try and hopefully it works for me

Paul
 
Upvote 0
Hi again - just to confirm it works perfectly - thank you so much for taken the time to reply - thankfully I can now move forward with the forms design

Paul
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,151
Members
453,021
Latest member
Justyna P

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