FormatPercent Function [VBA]

Contents

# FormatPercent [VBA]

Returns a string with a number formatting applied to a numeric expression. A percent sign is appended to the returned string.

This constant, function or object is enabled with the statement Option VBASupport 1 placed before the executable program code in a module.

### Syntax:

          FormatPercent( expression, [numDigitsAfterDecimal As Integer], [includeLeadingDigit As Integer], _
              [useParensForNegativeNumbers As Integer], [groupDigits As Integer] ) As String

### Return type:

String

### Parameters:

expression: Required. A numeric expression to be formatted. If expression is a string, then the decimal and thousands separator need to be localized.

numDigitsAfterDecimal: Optional. A numeric value specifying the number of digits that should be displayed after the decimal. If omitted, it defaults to the value -1, meaning that the default settings for user interface locale should be used.

includeLeadingDigit: Optional. A vbTriState enumeration value, specifying whether a leading zero should be displayed for fractional values.

- vbTrue or -1: Display a leading zero.

- vbFalse or 0: Do not display leading zeros.

- vbUseDefault or -2: Use the user interface locale settings. This is the default when omitted.

useParensForNegativeNumbers: Optional. A vbTriState enumeration value specifying whether negative numbers should be encased in parenthesis.

- vbTrue or -1: Use parenthesis for negative numbers.

- vbFalse or 0: Do not display parenthesis.

- vbUseDefault or -2: Same as vbFalse. This is the default when omitted.

groupDigits: Optional. A vbTriState enumeration value specifying the number should be grouped (into thousands, etc.), using the group delimiter that is specified on the system's regional settings.

- vbTrue or -1: Group digits.

- vbFalse or 0: Do not group digits.

- vbUseDefault or -2: Same as vbFalse. This is the default when omitted.

### Error codes:

13 Data type mismatch

### Example:

            Sub TestFormatNumber
              Const UseComputerRegionalSettings = -1

              MsgBox FormatPercent(12.2, NumDigitsAfterDecimal:=2) ' 1220.00% if selected user interface is english

              MsgBox FormatPercent("-,2", 2, IncludeLeadingDigit:=vbTrue) ' -20,00% if french user interface

              MsgBox FormatPercent("-0.2", 2) ' -20.00% for en-US, -0,00 for fr-CA, de-AT or pt-BR

              MsgBox FormatPercent(-0.2, UseComputerRegionalSettings, UseParensForNegativeNumbers:=vbTrue) ' (20,00)% if pt-BR

              MsgBox FormatPercent("-0,2", UseComputerRegionalSettings, vbUseDefault, vbTrue) ' (20,00)% if german

              MsgBox FormatPercent("-12345678", -1, vbUseDefault, vbUseDefault, GroupDigits:=vbTrue) ' -1 234 567 800,00% for fr-BE

            End Sub

Related Topics

VBA Mathematical functions

Please support us!