Set String Format Method
The Set String Format method returns an expression in a format that you specify. For details about how to use this method, see About Formatting Strings.
Format
Format[$](expression[, format])
For information about the dollar sign, see Usage of the Dollar Sign.
The following table describes the arguments that you can use with this method.
Argument | Description |
---|---|
expression |
The value that this method formats. This argument can contain one of the following items:
This method formats the value you enter as a number, date, time, or string depending on the format argument. To format a string, it transfers one character at a time from the value you enter in the expression argument to the output string. |
format |
A string expression that identifies the format that this method uses. You must use quotation marks ("") to enclose the value in the format argument. |
Usage
The Set String Format method formats a numeric value as a number or date and time. If you specify a numeric expression, and if you do not include the format argument, or if the format argument is null, then this method converts the number to a string without any special formatting.
It can format a numeric value or a variant as a date. If it formats a numeric value as a date, then it interprets the value according to standard Visual Basic date encoding. The starting date is December 30, 1899. It represents this date as zero. It represents subsequent dates as the number of days from the starting date.
If the format string does not match the Regional Settings, or if the Date in the Microsoft Windows setting is not set to the U.S. format, then the Set String Format method does not return the correct format.
Example 1
The following example includes tokens:
Sub Button1_Click
Dim msgtext As String
msgtext = Format("Section #AB-234", "<\[&&&&&&&&&&&&&&&\]") _
& Chr$(13) & Chr$(13) & Format("incoming", ">@@@@@@@@\!\!") _
& Chr$(13) & Chr$(13) _
& Format("Profits are expected to rise.", _
"!&&&&&&&&&&&&&&&&&")
End Sub
The following example calculates the square root of 2 as a double-precision, floating point value, and then displays it in scientific notation:
Sub Button1_Click
Dim value As Double
Dim msgtext As String
value = CDbl(Sqr(2))
msgtext = "The square root of 2 is " & Format(value, "Scientific")
End Sub
Example 3
The following example uses multiple tokens to format the result of the Now method, which returns the current date and time on the computer clock:
Sub ClickMe_Click
dim msgtext As String
msgtext = Now & Chr$(13) & Chr$(13) _
& "Today is " & Format(Now, "dddd") & ", " _
& Format(Now, "mmmm") & " " & Format(Now, "dd") & ", " _
& Format(Now, "yyyy") & "." _
& Chr$(13) & "The time is " & Format(Now, "h:nn am/pm") _
& " and " & Format(Now, "s") & " seconds."
End Sub
For other examples, see the following topics: