How to use datediff function with user form

We can use the datediff function with a user-form to quickly and easily calculate the difference between two dates in days, months and years.

How to use datediff function with user-form
How to use datediff function with user-form

Watch the video below:

Watch this video on YouTube.

Here’s the complete code for the command button that executes the datediff function and an easy method to populate the combo-box:

Private Sub CommandButton1_Click()
nextBlankRow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
TextBox3.Value = DateDiff(“d”, (TextBox1.Text), (TextBox2.Text))
Cells(nextBlankRow, 1) = TextBox1.Value
Cells(nextBlankRow, 2) = TextBox2.Value
Cells(nextBlankRow, 3) = TextBox3.Value
Cells(nextBlankRow, 4) = ComboBox1.Value

End Sub

Private Sub UserForm_Initialize()
With ComboBox1
.AddItem “A”
.AddItem “B”
.AddItem “C”
End With

End Sub

Further reading:


Datediff function

Last row, last column, last cell

Next Blank row