VBA Now() - danielep71/VBA-PERFORMANCE GitHub Wiki

[Home]] ](/danielep71/VBA-PERFORMANCE/wiki/[[Class-cPerformanceMonitor)

VBA Now Function is a built-in function in MS Excel:

  • It does not take any input arguments or parameters.
  • It returns the current system date and time.
  • It is a ‘Data and Time’ type function. The default format of the Now Function is ‘mm/dd/yyyy HH:MM:SS AM/PM’.

1. The Date data type

Date variables can store any value between January 1, 100, through December 31,999, as well as any time value.

They take 8 bytes, exactly like Double variables. This isn’t a casual resemblance because, internally, these date/time values are stored as floating-point numbers in which the integer part stores the date information (Date() Function) and the decimal part stores the time information (Time() Function).

Once you know how Date variables store their values, you can perform many meaningful math operations. For example, you can truncate date or time information using the Int Function as follows:

  • MyVar = Now() ‘MyVaR is a date variable
  • DateVar = Int(MyVaR) ‘Extract date information
  • TimeVar = MyVaR – Int(MyVar) ‘Extract Time information
  • MyVar = MyVar + 7 ‘Advance one week
  • MyVaR = MyVar – 365 ‘Go back one (nonleap) year

2. Now() to measure time intervals

We can also use the VBA Now function to measure the time elapsed in Hours, Minutes, and Seconds between two time intervals:

Typical use of the Now() Function

Dim currTime as Date 
currTime = Now()
'... your code here ...
Debug.Print Format(currTime  - Now(), "hh:nn:ss")

Result for 1 minute and 5 seconds: 00:01:05

[Home]] ](/danielep71/VBA-PERFORMANCE/wiki/[[Class-cPerformanceMonitor)