In this article, Iâ€™ll show you how you can format a percentage to 2 decimal places using **VBA** in Excel. Youâ€™ll learn to format a percentage using **VBA** with examples involving a** Macro **and a **User-Defined function** (**UDF**).

Format Percentage to 2 Decimal Places with Excel VBA (Quick View)

```
Sub Percentage_to_2_Decimal_Places()
First_Number = 102
Second_Number = 151
Percentage = First_Number / Second_Number
Percentage = Format(Percentage, "0.00%")
End Sub
```

**Download Practice Workbook**

Download this practice workbook to exercise while you are reading this article.

**How to Format Percentage to 2 Decimal Places with Excel VBA (Step-by-Step Analysis)**

Without further delay, letâ€™s go to our main discussion today. Weâ€™ll learn to format a percentage to 2 decimal places.

Letâ€™s try to format the percentage of **102** with respect to **151** to 2 decimal places. Iâ€™m showing you the step-by-step procedure to accomplish this.

**â§Ş Step 1: Inserting the Inputs**

First of all, weâ€™ve to insert the inputs to calculate the percentage, Here they are **102 **and **151**.

```
First_Number = 102
Second_Number = 151
```

**â§Ş Step 2: Calculating the Percentage**

Next, weâ€™ll have to calculate the percentage. Weâ€™ll divide the first number by the second number to achieve this.

`Percentage = First_Number / Second_Number`

**â§Ş Step 3: Formatting to 2 Decimal Places**

This is the most important step. Weâ€™ll use the **Format function** of **VBA** to convert the percentage to **2** decimal places.

`Percentage = Format(Percentage, "0.00%")`

So, the complete **VBA** code will be:

**â§ VBA Code:**

```
Sub Percentage_to_2_Decimal_Places()
First_Number = 102
Second_Number = 151
Percentage = First_Number / Second_Number
Percentage = Format(Percentage, "0.00%")
End Sub
```

**â§ Output:**

Run the code with the line **MsgBox Percentage** (Obviously by changing the inputs according to your needs).

Itâ€™ll show the percentage of the first number with respect to the second number to **2** decimal places. Here itâ€™s **67.55%.**

**Examples to Format Percentage to 2 Decimal Places with Excel VBA (Macro and UDF)**

Weâ€™ve learned to percentage to **2** decimal places using **VBA** in Excel. Now weâ€™ll explore a few examples to make things pretty clear.

**1. Developing a Macro to Format a Range of Percentage to 2 Decimal Places**

Here weâ€™ve got a data set with the names of some products (**B4:B13**), their target sales(**C4:C13**), the achieved sales (**D4:D13**), and an empty column (**E4:E13**) for the** percentage** of the achieved sales with respect to the target sales of a company.

Our objective today is to develop a **Macro** thatâ€™ll calculate the **percentage** of the **achieved sales **with respect to the **target sales** to **2** decimal places.

So, the complete **VBA** code will be:

**â§ VBA Code:**

```
Sub Percentages_to_2_Decimal_Places()
Set First_Range = Range("D4:D13")
Set Second_Range = Range("C4:C13")
Set Output_Range = Range("E4:E13")
For i = 1 To Output_Range.Rows.Count
Â Â Â For j = 1 To Output_Range.Columns.Count
Â Â Â Â Â Â Â Output_Range.Cells(i, j) = First_Range.Cells(i, j) / Second_Range.Cells(i, j)
Â Â Â Â Â Â Â Output_Range.Cells(i, j) = Format(Output_Range.Cells(i, j), "0.00%")
Â Â Â Next j
Next i
End Sub
```

**â§ Output:**

Run the code (Donâ€™t forget to change the inputs according to your need.). Itâ€™ll put the percentage of the achieved sales with respect to the target sales in the range **E4:E13** of the active worksheet.

**Read More:** **Excel VBA: Round to 2 Decimal Places (5 Ways)**

**2. Creating a User-Defined Function to Format Percentage to 2 Decimal Places with Excel VBA**

Weâ€™ve learned to develop a **Macro** to format a range of percentages to 2 decimal places. Now weâ€™ll create a user-defined function to accomplish this.

Therefore, the complete **VBA** code will be:

**â§ VBA Code:**

```
Function PERCENTAGE(First_Range As Range, Second_Range As Range)
Dim Output_Range() As Variant
ReDim Output_Range(First_Range.Cells.Count - 1, First_Range.Columns.Count - 1)
For i = 1 To First_Range.Rows.Count
Â Â Â For j = 1 To First_Range.Columns.Count
Â Â Â Â Â Â Â Output_Range(i - 1, j - 1) = First_Range.Cells(i, j) / Second_Range.Cells(i, j)
Â Â Â Â Â Â Â Output_Range(i - 1, j - 1) = Format(Output_Range(i - 1, j - 1), "0.00%")
Â Â Â Next j
Next i
PERCENTAGE = Output_Range
End Function
```

**â§ Output:**

Select any cell of the worksheet and enter the formula:

**=PERCENTAGE(D4:D13,C4:C13)**

Itâ€™ll calculate the percentages of the range **D4:D13** with respect to the range **C4:C13** and format the percentages to **2 **decimal places.

**Array Formula**. So donâ€™t forget to select the entire range before entering the formula and press

**CTRL+SHIFT+ENTER**to enter it).

**Read More:** **Excel VBA: Format Currency to Two Decimal Places (3 Methods)**

**Things to Remember**

Here Iâ€™ve focused on formatting to **2** decimal places only. If you want to format any other decimal places, use the **Format function** accordingly.

For example, to format to** 1** decimal place, use **â€ś0.0%â€ť **within the **Format function**.

Also while working with **VBA** in Excel, you neednâ€™t save any file to run a **VBA** code. You can see the output of any code without saving the file. But in case you want to save the file for future use, you must save the file as** Excel Macro-Enabled Workbook** (***.xlsm**).

**Conclusion**

So, these are the ways to format a percentage to 2 decimal places with **VBA** in Excel. Do you have any questions? Feel free to ask us. And donâ€™t forget to visit our site **ExcelDemy** for more posts and updates.