Friday, August 12, 2016

How to use the FORMAT Function with Dates (VBA)

Description

The Microsoft Excel FORMAT function takes a date expression and returns it as a formatted string.

Syntax

The syntax for the FORMAT function in Microsoft Excel is:
Format ( expression, [ format, [ firstdayofweek, [firstweekofyear] ] ] )

Parameters or Arguments

expression
The date value to format.
format
Optional. It is the format to apply to the expression. You can either define your own format or use one of the named formats that Excel has predefined such as:
FormatExplanation
General DateDisplays date based on your system settings
Long DateDisplays date based on your system's long date setting
Medium DateDisplays date based on your system's medium date setting
Short DateDisplays date based on your system's short date setting
Long TimeDisplays time based on your system's long time setting
Medium TimeDisplays time based on your system's medium time setting
Short TimeDisplays time based on your system's short time setting
firstdayofweek
Optional. It is a value that specifies the first day of the week. If this parameter is omitted, it assumes that Sunday is the first day of the week. This parameter can be one of the following values:
ConstantValueExplanation
vbUseSystem0Uses the NLS API setting
VbSunday1Sunday (default, if parameter is omitted)
vbMonday2Monday
vbTuesday3Tuesday
vbWednesday4Wednesday
vbThursday5Thursday
vbFriday6Friday
vbSaturday7Saturday
firstweekofyear
Optional. It is a value that specifies the first week of the year. If this parameter is omitted, it assumes that the week that contains January 1 is the first week of the year. This parameter can be one of the following values:
ConstantValueExplanation
vbUseSystem0Uses the NLS API setting
vbFirstJan11The week that contains January 1
vbFirstFourDays2The first week that has at least 4 days in the year
vbFirstFullWeek3The first full week of the year

Applies To

  • Excel 2016, Excel 2013, Excel 2011 for Mac, Excel 2010, Excel 2007, Excel 2003, Excel XP, Excel 2000

Type of Function

  • VBA function (VBA)

Example (as VBA Function)

The FORMAT function can only be used in VBA code in Microsoft Excel.
Let's look at some Excel FORMAT function examples and explore how to use the FORMAT function in Excel VBA code:
Format(#17/04/2004#, "Short Date")
Result: '17/04/2004'

Format(#17/04/2004#, "Long Date")
Result: 'April 17, 2004'

Format(#17/04/2004#, "yyyy/mm/dd")
Result: '2004/04/17'
For example:
Dim LValue As String

LValue = Format(Date, "yyyy/mm/dd")
In this example, the variable called LValue would now contain the date formatted as yyyy/mm/dd.

No comments:

Post a Comment