46. VBA function (date, DateAdd)
Some of this topic was processed by machine translation. Original
Date and time are very special among data types, and the use of functions is essential.
This section explains the list of VBA functions related to dates and the DateAdd function.
For other functions other than the DateAdd function, refer to the individual pages from the links in the list.
List of VBA functions related to date and time
|Function||Description||Supported worksheet functions|
|Date||Returns the current system date value||Same as worksheet function TODAY|
|Now||Returns the current date and time value||Same as worksheet function|
|Time||Returns the current system time value|
|Timer||Returns the number of seconds since midnight as a single-precision floating-point number
Used when measuring up to 1/100.
|Year||Returns the year value||Same as worksheet function|
|Month||Returns a value between 0 and 12 representing the month of the year||Same as worksheet function|
|Day||Returns a value between 1 and 31 representing the day of the month||Same as worksheet function|
|Hour||Returns a value between 0 and 23 representing the time of day||Same as worksheet function|
|Minute||Returns a value between 0 and 59 representing the minute of the time||Same as worksheet function|
|Second||Returns a value in the range 0-59 representing seconds of time||Same as worksheet function|
|Weekday||Returns the day of week value||Same as worksheet function|
|WeekdayName||Returns a string representing the specified day of the week|
|MonthName||Returns a string representing the specified month|
|DatePart||Returns a value containing the specified part of a date|
|DateSerial||Returns the value of the date corresponding to the year, month, and day specified in the argument||Same as DATE for worksheet function|
|DateValue||Returns a date value||Same as worksheet function|
|TimeSerial||Returns the time value corresponding to the hour, minute, and second specified by the argument||Same as TIME for worksheet function|
|TimeValue||Returns a value representing the time||Same as worksheet function|
|DateAdd||Returns a date value with the specified time interval added|
|DateDiff||DateDiff returns the value of the time interval between two specified dates||Same as worksheet function DATEDIF
However, the order of the arguments is different
Many have corresponding worksheet functions, so the explanation is omitted here.
If you don't know the corresponding worksheet function, remember that first.
There is no directly corresponding worksheet function,
The DateAdd function is often used in macro VBA.
DateAdd function syntax
|interval||Must be specified.
Specifies a string expression that represents the time interval to be added.
|number||Must be specified.
Specifies a formula that represents the number of time intervals to add. Specify a positive number to get a future date and time. Specify a negative number to get the past date and time.
|date||Must be specified.
Specifies a Variant (variant of internal processing format Date) value or literal string that represents the date to add the time interval.
String expression specified for interval
|y||Day of year|
Example of using DateAdd function
Cells(1, 2) = DateAdd("d", 3, Cells(1, 1))
Cells(1, 3) = DateAdd("m", 1, Cells(1, 1))
When you run this
To B1 cell, 2012/1/31
In C1 cell, 2012/2/28
This will be the result.
Date functions are easy to remember because many of them are the same as worksheet functions.
Especially in the paperwork, please keep in mind that date processing is inevitable.
Articles with the same theme "VBA basics"
43. General practice question 5
44. About VBA functions
45. VBA function (Format)
46. VBA function (date, DateAdd)
47. VBA function (String operation, Replace, InStr, StrConv)
48. VBA function (Other, Fix, Int, Rnd, Round, IsEmpty)
49. Like operator and wildcard
50. General practice question 6
51. With statement
52. Object variables and Set statements
53. Workbook object
I'm going to pay close attention to the description,
If you have any mistakes or suggestions,「Contact Us」I hope you will let me know.
Use the posted VBA code at your own risk.
We are not liable for any damages such as data corruption.