Source: http://mcdelfino.blog.51cto.com/2058744/691026
Dim Runtime As Date 'defines the time variable Sub RunTimer () Runtime = Now () + TimeValue ("00:00:02")'. The time is displayed As an increase of 1 second Application. onTime Runtime, "my_Procedure" 'execute the following process at the specified Time: End Sub my_Procedure () Range ("A1") = Format (Time, "h: mm: ss ") 'display the current system Time (Time) RunTimer in the specified table 'and continue executing the above program End Sub
The OnTime function is used to execute a specific process at a specific time, that is, the process my_Procedure.
When my_Procedure is executed, the current time is displayed, and the Runtime process is executed immediately, and then the loop goes down. The effect is shown in: (you can set the color size separately)
Concepts involved:
- Date data type: Indicates the time from January 1, January 1-9, 0001, December 31, 999, and from midnight 12:00:00 to 11:59:59. 9999999. The default value of Date is January 1, 0001 0:00:00 (midnight ).
- Now Function: Returns the current date and time in datetime format.
- Time Function: Returns the current system time.
- TimeValue Function: Converts the time in text format to the time in date and time format.
- Application. OnTime Function: Schedule a process to run at a specific time in the future (either after a specific time or after a specified time ).
OnTime (EarliestTime,Procedure,LatestTime,Schedule)
The following two parameters are optional!The method of Application. OnTime Runtime, "my_Procedure" for a single row cannot be enclosed by brackets. If the assignment operation is involved, brackets are required!
- Worksheet. Cells attributes: When no parameter is set, it indicates all cells in the worksheet. If one parameter is set, the cells in the first row are displayed. If two parameters are set, the rows and columns are displayed!
With Cells(2, 2).Font .Size = 20 .Color = RGB(255, 0, 0) .Name = "Arial Black" .FontStyle = "Bold Italic" End With
- Worksheet. Range attribute: Indicates a cell or cell area.
- Format Function: Return according to the formatStringA string containing the instruction setting format in the expression. The format contains predefined symbols, which indicate different display formats!