# Calculating the no. of days between two dates on a user form

#### Mlbblue

##### New Member
This is probably very easy but I'm totally stuck on it...

I've got a user form with three fields, the first two fields are for date A and date B which is entered by the user.

Upon exit of field B (after date B inputted) I want field C to show the number of days between dates A and B in fields A and B.

Afterwards the user will be submitting these to a sheet (no problems with that)

I'm totally stuck on how to automatically make field C show the days between dates A and B.

Is there anyone out there that can help me?

Any help is appreciated!

Cheers

Mark

### 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.

#### kkknie

##### Well-known Member
In your userform, enter the following (changing TextBox1,2,3 to whatever names you have given them).
Code:
``````Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Call DoDateDiff
End Sub

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Call DoDateDiff
End Sub

Sub DoDateDiff()

Dim tmpD1 As String
Dim tmpD2 As String
Dim D1 As Date
Dim D2 As Date
Dim rDelta As Single

tmpD1 = Trim(TextBox1.Text) 'Get rid of extra spaces
tmpD2 = Trim(TextBox2.Text) 'Get rid of extra spaces

If tmpD1 = "" Then Exit Sub
If tmpD2 = "" Then Exit Sub

If Not IsDate(tmpD1) Then Exit Sub
If Not IsDate(tmpD2) Then Exit Sub

D1 = CDate(tmpD1)
D2 = CDate(tmpD2)

rDelta = D2 - D1

TextBox3.Text = CLng(rDelta)

End Sub``````
When the user exits either the first or second box, the DoDateDiff routine will run and calculate a date if it is possible to calculate (i.e. the inputs are valid dates).

Hope this helps,

K

#### Mlbblue

##### New Member
Thanks K, that's exactly what I was looking for. You're a life saver!

Cheers

Mark

Replies
7
Views
88
Replies
7
Views
69
Replies
19
Views
324
Replies
4
Views
257
Replies
1
Views
229