Excel VBA FormatPercent Function

VBA FormatPercent Function: The FormatPercent function in VBA is used to apply a percent format to a numeric expression, and it returns the result as a string.

Syntax

FormatPercent (Expression, [NumDigitsAfterDecimal], [IncludeLeadingDigit],
[UseParensForNegativeNumbers], [GroupDigits])

Parameter

Expression (required) – This parameter represents the expression that you want to format.

NumDigitsAfterDecimal (optional) – This parameter represents the number of digits that should be shown after the decimal. The default value is -1.

IncludeLeadingDigit (optional) – This parameter represents the vbTriState enumeration value, stating whether a leading zero should be shown for fractional values. The default value is set to vbUseDefault.

It can take the following values:

  • vbFalse – It does not display a leading zero.
  • vbTrue – It displays a leading zero.
  • vbUseDefault (default)– It uses the default computer settings.

UseParensForNegativeNumbers (optional) – This parameter represents the vbTriState enumeration value, stating whether negative numbers should be encased within parentheses. The default value is set to vbUseDefault.

It can take the following values:

  • vbFalse – It does not encase negative numbers in parentheses.
  • vbTrue – It encases the negative numbers in parentheses.
  • vbUseDefault (default)– It uses the default computer settings.

GroupDigits (optional) – This parameter specifies whether the number should be grouped (into thousands, etc.), using the group delimiter that is specified on the computer's regional settings. The default value is set to vbUseDefault.

It can take the following values:

  • vbFalse – It does not group digits.
  • vbTrue – It groups the digits.
  • vbUseDefault (default)– It uses the default computer settings.

Return

This function returns a string value after applying the percentage format to the supplied numeric expression.

Example 1

Sub FormatPercent_Example1()
 ' Formatting the numeric values with percentage formats.
 Dim formatpercent_var As String
 formatpercent_var = formatpercent(20)
 '' The variable formatpercent_var will return the String "2000.00%".
 Cells(1, 1).Value = formatpercent_var
 End Sub 

Output

2000.00%

VBA FormatPercent Function

Example 2

Sub FormatPercent_Example2()
 ' Formatting the numeric values with percentage formats.
 Dim formatpercent_var As String
 formatpercent_var = formatpercent(-20, , vbTrue)
 ' The variable formatpercent_var will return the String "-2000.00%".
 Cells(1, 1).Value = formatpercent_var
 End Sub 

Output

-2000.00%

VBA FormatPercent Function

Example 3

Sub FormatPercent_Example3()
 ' Formatting the numeric values with percentage formats.
 Dim formatpercent_var As String
 formatpercent_var = formatpercent(10.559, 1)
 '' The variable formatpercent_var will return the String "1055.90%".
 Cells(1, 1).Value = formatpercent_var
 End Sub 

Output

1055.90%

VBA FormatPercent Function

Example 4

Sub FormatPercent_Example4()
 ' Formatting the numeric values with percentage formats.
 Dim formatpercent_var As String
 'initializing with string "Hello VBA"
 formatpercent_var = formatpercent("Hello VBA", 0)
 '' The variable formatpercent_var type mis match run time error.
 Cells(1, 1).Value = formatpercent_var
 End Sub 

Output

VBA FormatPercent Function