Excel vba date and time - Examples of Date and Time Functions


Learn how to work with dates and times in Excel VBA.


In this chapter Learn how to work with date and time in Excel VB (Visual Basic) for Beginners.



Example 1

Place a command button on your worksheet and add the code lines below. To execute the code lines, click the command button on the sheet.

Sub dateexample()
Dim expdate As Date
expdate = DateValue("jun  08, 2015")
MsgBox expdate
End Sub

Out put.



Example 2

To know current date and time in Excel VBA below code represent this example.

Sub dateexample()

MsgBox Now

End Sub

Outp put.





Example 3

Now, to clearly see that Excel handles date and times internally as text between date and time, add the following code lines:.

Sub dateexample()
Dim exptime As Date
Dim expdate As Date
exptime = Time
expdate = Date

MsgBox "Today " & expdate & " Time  " & exptime

End Sub 

Out put




Note: instead of given string value(Today and time) you can modify as you want in between this symbol " " Excel VBA won't consider this as codes. 
If you want to output this result to Excel worksheets cell or cells, Please refer Range and cells:



Do you Like this page..?? Please give your Email id and Subscribe below for Daily news letter or like this page on Facebook..!! 


Excel vba date and time - Examples of Date and Time Functions Excel vba date and time - Examples of Date and Time Functions Reviewed by Unknown on 23:10 Rating: 5

No comments:

Powered by Blogger.