Author Archives: Dinesh Kumar Takyar

About Dinesh Kumar Takyar

Welcome to exceltrainingvideos.com! My aim is to help you learn MS Excel including VBA. I always felt that a good way to learn something was to find solutions to problems in that domain. That is why I share these Excel videos with you. Mostly these are questions asked during my corporate training sessions. I've been training individuals and companies in computers since 1991. I did my B. Sc. (Vordiplom), M. Sc. (Diplom) and Ph.D. (Dr. rer. nat.) from Hamburg, Germany. The best thing about solving some of my visitor's questions is that I also gain new insights. For more Excel VBA solutions you may like to visit my YouTube channel: http://youtube.com/familycomputerclub For a structured Excel VBA training course online you can visit: https://www.youtube.com/excelvbaonline

Format Function with Dates VBA

How to use the FORMAT Function with Dates in VBA. Working with dates in Excel is useful, important and confusing. Dates are formatted in Excel using month/day/year format. If you use another formatting like day/month/year, transfer of data from worksheet to worksheet, workbook to workbook or from user-form to worksheet can be confusing. The date formatting function can bring some sanity into the process also during calculations using functions like datediff. Watch the video below:

Watch the video on YouTube.

Format Function with dates VBA

Format Function with dates VBA

Here’s the complete VBA code:

Private Sub CommandButton1_Click()
Dim date1 As Date, date2 As Date
Dim erow As Long

date1 = Format(TextBox1.Text, “dd-mm-yyyy”)
date2 = Format(TextBox2.Text, “dd-mm-yyyy”)
If date1 > date2 Then
TextBox1.SetFocus
MsgBox “The start date cannot be greater than the end date!”
Exit Sub
End If

TextBox3.Text = DateDiff(“d”, date1, date2)

erow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
Cells(erow, 1) = date1
Cells(erow, 1) = Format(Cells(erow, 1), “Long Date”)
Cells(erow, 2) = date2
Cells(erow, 2) = Format(Cells(erow, 2), “Long Date”)
Cells(erow, 3) = TextBox3.Value

End Sub

Further reading:

How to use the FORMAT Function with Dates (VBA)

Datediff function in user-form