Excel VBA Format Textbox as Percentage
In this article
To format a TextBox as a percentage in Excel VBA, you do not change the TextBox's own properties—you convert the numeric value you assign to its .Text property into a percentage string, or convert the user's typed percentage back into a decimal number when reading it. The TextBox control has no built-in number format, so the formatting happens in your code, typically using the Format function or the Val function for the reverse conversion.
Why a TextBox Has No Percentage Format
Unlike worksheet cells, which have a NumberFormat property that can be set to 0.00%, a UserForm TextBox is a text container. It stores whatever characters are typed, including the percent sign, spaces, or letters. VBA does not interpret the content as a number unless you explicitly convert it. This means you must handle the formatting and parsing yourself, both when displaying a value and when reading the user's input.
This design is intentional: TextBoxes are meant for free-form input. If you need a strictly numeric or percentage entry, you either validate the input in the Change or Exit event, or you use a different control like a SpinButton combined with a Label. For most cases, a TextBox with manual conversion is the simplest and most flexible approach.
Displaying a Decimal as a Percentage
Suppose you have a decimal value like 0.125 and you want the TextBox to show 12.5%. The Format function is the standard tool:
Me.TextBox1.Text = Format(0.125, "0.0%")The format string 0.0% multiplies the value by 100, adds the percent sign, and shows one decimal place. You can adjust the decimals: 0% for whole percentages, 0.00% for two decimals. The percent sign in the format string is literal—it does not need escaping.
If your value is stored as a whole number like 12.5 (meaning 12.5%), you must divide by 100 before formatting:
Me.TextBox1.Text = Format(12.5 / 100, "0.0%")This is a common mistake: assuming Format(12.5, "0.0%") gives 12.5%—it actually gives 1250.0% because the format multiplies by 100.
Reading a Percentage Back into a Decimal
When the user types 12.5% into the TextBox, you need to convert it back to 0.125 for calculations. The safest method is to strip the percent sign and divide by 100:
Dim pct As Double
pct = Val(Replace(Me.TextBox1.Text, "%", "")) / 100The Replace function removes the percent sign, Val reads the leading numeric part (ignoring trailing text), and dividing by 100 yields the decimal. This works even if the user types 12.5 % with a space, because Val stops at the space—but it will read 12.5 correctly.
If the user might type a negative percentage, Val handles the minus sign. If they type a value like .125 (thinking it is already a decimal), your code would interpret it as 0.125%, which is likely wrong. You should decide whether your TextBox expects whole-number percentages (e.g., 12.5) or decimal fractions (e.g., 0.125) and validate accordingly.
Validating User Input in the Exit Event
To prevent invalid entries, handle the TextBox's Exit event. This fires when the user tabs away or clicks another control. A simple validation routine can reject non-numeric input or automatically reformat what was typed:
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim val As Double
If IsNumeric(Replace(Me.TextBox1.Text, "%", "")) Then
val = Val(Replace(Me.TextBox1.Text, "%", "")) / 100
Me.TextBox1.Text = Format(val, "0.0%")
Else
MsgBox "Please enter a valid percentage."
Cancel = True
End If
End SubThis reformats the input as the user leaves the field, so they always see a consistent percentage format. The Cancel = True keeps focus on the TextBox if the input is invalid. Note that IsNumeric alone will accept 12.5% as numeric? Actually, IsNumeric("12.5%") returns False in VBA, so the Replace is essential.
Common Mistakes and How to Avoid Them
- Forgetting the division by 100: When reading a percentage string, always divide by 100 to get the decimal. When displaying a decimal, always multiply by 100 (the
Formatfunction does this automatically). - Using
CDblon a string with a percent sign:CDbl("12.5%")throws a type mismatch error. Always remove the percent sign first. - Assuming the TextBox has a
NumberFormatproperty: It does not. Trying to setTextBox1.NumberFormatwill cause a compile error. - Not handling empty input: If the user clears the TextBox,
Val("")returns 0, which may be acceptable, but you might want to treat it as null.
Alternative: Using a SpinButton or Slider
If you want to avoid typing altogether, pair a TextBox with a SpinButton control. Set the SpinButton's Min and Max to represent hundredths of a percent (e.g., 0 to 10000 for 0% to 100% with two decimals). In the SpinButton's Change event, update the TextBox:
Private Sub SpinButton1_Change()
Me.TextBox1.Text = Format(SpinButton1.Value / 10000, "0.00%")
End SubThis gives the user a clickable control that always produces a valid percentage. The downside is that typing is not allowed, which may be less convenient for large ranges.
Formatting in a ListBox or ComboBox
If you are displaying percentages in a ListBox or ComboBox, the same principle applies: the control stores strings, not formatted numbers. You must format each item when you add it:
Me.ListBox1.AddItem Format(0.125, "0.0%")When the user selects an item, parse it back using the same Replace and Val method. This is often easier than a TextBox because the user cannot type invalid data, but you still need the conversion for calculations.
Frequently Asked Questions
How do I show a percentage without decimals in a TextBox?
Use the format string 0%. For example, Format(0.125, "0%") returns 13% (rounds to nearest whole percent). If you need to round down, use Int or WorksheetFunction.RoundDown before formatting.
Why does my TextBox show 1250% instead of 12.5%?
Because you passed a whole number to Format with a percent format. The format multiplies by 100. Divide your whole number by 100 first: Format(12.5 / 100, "0.0%").
Can I use FormatPercent function instead?
Yes, FormatPercent(0.125, 1) returns 12.5%. It is simpler but less flexible—you cannot control trailing zeros as precisely, and it always uses the system locale's percent symbol.
How do I handle a user typing "12%" vs ".12"?
Decide on one convention. If you expect whole-number percentages, check whether the input contains a percent sign. If it does, divide by 100; if not, treat the number as already a decimal (e.g., .12). You can also automatically divide by 100 if the absolute value is greater than 1, but that can be confusing—better to validate explicitly.
Is there a way to make the TextBox accept only numbers and a percent sign?
Yes, in the KeyPress event, you can reject characters that are not digits, a decimal point, a minus sign, or the percent sign. However, this does not prevent multiple percent signs or invalid order; full validation in the Exit event is more reliable.
What if I need to show a percentage in a UserForm label?
Labels also have no number format. Use the same Format function when setting the .Caption property: Me.Label1.Caption = Format(0.125, "0.0%").