O'Reilly logo

VB & VBA in a Nutshell: The Language by Paul Lomax

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Format, Format$ Functions

Named Arguments

No

Syntax

Format(expression[, format[, firstdayofweek[, _
       firstweekofyear]]])

expression

Use: Required

Data Type: String/Numeric

Any valid string or numeric expression.

format

Use: Optional

A valid named or user-defined format expression.

firstdayofweek

Use: Optional

Data Type: Numeric

A constant that specifies the first day of the week.

firstweekofyear

Use: Optional

Data Type: Numeric

A constant that specifies the first week of the year.

Return Value

A variant of subtype string containing the formatted expression.

Description

Allows you to use either predefined or user-defined formats to create an infinite variety of ways to output string, numeric, and date/time data. It's possibly the most complex single function call in VB.

Rules at a Glance

  • See CDate for an explanation of the firstdayofweek and firstweekofyear arguments.

  • format can be either a predefined or a user-defined format.

  • User-defined formats for numeric values are created with up to four sections. Each section is used for a different type of numeric value and is delimited with a semicolon. The four possible sections are shown in the following table:

    Section Applies to
    1 All values, if used alone; positive values, if used with more than one section
    2 Negative values
    3 Zero values
    4 Null values
  • It's not necessary to include all four sections in the format clause. However, the number of sections present determines what types of numeric values each section defines, as the following ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required