VBA  XVII- Date and Time

 

Sometimes it is important to display date and time when we create our Excel documents. It is relatively easy to create a VBA that can display date and time. In this program, I have designed a VBA that can display current year, current month, current date, current weekday, current hour, current minute and even current second. Here is the program:

Private Sub CommandButton1_Click()

Cells(2, 2) = DatePart("yyyy", Now)
Cells(3, 2) = DatePart("m", Now)
Cells(4, 2) = DatePart("d", Now)
Cells(5, 2) = DatePart("w", Now)
Cells(6, 2) = WeekdayName(Weekday(Date))
Cells(7, 2) = DatePart("h", Now)
Cells(8, 2) = DatePart("n", Now)
Cells(9, 2) = DatePart("s", Now)


End Sub


 


Explanation:

DatePart("yyyy",Now) displays current year, DatePart("m", Now) displays current month, DatePart("d", Now) displays current day, DatePart("w", Now) displays current week, WeekdayName(Weekday(Date)) displays current day of the week, DatePart("h", Now) displays current hour, DatePart("n", Now) displays current minute and DatePart("s", Now) displays second.

 

 

 

 

 

 [Back to VBToday]