Percent of Number in Excel with a Macro
In this article
To calculate a percent of a number in Excel with a macro, you write a Sub that reads a value and a percentage from cells, multiplies them, and optionally writes the result with correct formatting. The shortest working version is Sub PctOfNum() — if B2 already holds 25%, the result in C2 is 25% of A2. The steps below turn that one line into a reusable macro with input boxes, validation and proper number formatting. Excel's menu labels can differ slightly by version, platform and region, so if you see a close match rather than the exact wording, you are in the right place.
Range("C2").Value = Range("A2").Value * Range("B2").Value
End Sub
What You'll Need
- A desktop copy of Excel for Windows or Mac. Macros do not run in Excel for the web or in the mobile apps.
- The workbook saved as an .xlsm (macro-enabled) file, or at least permission to save it that way.
- The Developer tab visible, or willingness to open the VBA editor with the keyboard shortcut instead.
- A sheet with a number in one cell and a percentage in another (for example A2 = 250, B2 = 20%).
- Macro security settings permissive enough to run your own code — the default "Disable all macros with notification" setting is fine because you can click Enable Content on opening.
Step-by-Step
- Set up your input cells. In A2 type the base number, for example 250. In B2 type the percentage as 20% (including the percent sign). Excel stores 20% as 0.2 behind the scenes, which is exactly what a multiplication needs.
- Open the VBA editor. Press Alt + F11 on Windows, or Option + F11 on Mac. If the shortcut does not respond, enable the Developer tab via File > Options > Customize Ribbon (Windows) or Excel > Settings > Ribbon & Toolbar (Mac), then click Developer > Visual Basic.
- Insert a module. In the editor choose Insert > Module. A blank code window appears. Modules keep your macro separate from sheet-specific code, which makes it easier to reuse and edit.
- Paste the basic macro. Type or paste:
Sub PercentOfNumber() Dim baseVal As Double Dim pctVal As Double baseVal = Range("A2").Value pctVal = Range("B2").Value Range("C2").Value = baseVal * pctVal Range("C2").NumberFormat = "0.00" End SubThe last line sets the output to two decimal places so 52.5 shows as 52.50 rather than being rounded or shown in odd formats. - Run it. Click anywhere inside the code and press F5, or use Run > Run Sub/UserForm. Switch back to the sheet: C2 should now hold 50 (250 × 0.2). If you see 0 or an error, check that B2 really is a number and not text — more on that below.
- Make it ask for inputs. Hard-coded cell references quickly become limiting. This version prompts the user:
Sub PercentPrompt() Dim baseVal As Variant, pctVal As Variant baseVal = InputBox("Enter the number:", "Percent Calculator") If Not IsNumeric(baseVal) Then Exit Sub pctVal = InputBox("Enter the percentage (e.g. 20 or 20%):", "Percent Calculator") If Not IsNumeric(pctVal) Then Exit Sub MsgBox baseVal * pctVal, vbInformation, "Result" End SubInputBox returns text, so a user typing 20 gives 0.2 only if they also type the percent sign. Simplest handling: tell users to enter the decimal (0.2) or divide by 100 in code. - Guard against a bare percentage entry. Replace the MsgBox line with logic that treats anything above 1 as a whole percentage:
If pctVal > 1 Then pctVal = pctVal / 100 MsgBox baseVal * pctValThis means 20 and 0.2 both produce the correct answer. - Apply the macro to a whole column. For a range of numbers in A2:A20 with percentages in B2:B20, loop instead of repeating the code:
Sub PercentColumn() Dim r As Long For r = 2 To 20 If IsNumeric(Cells(r, 1).Value) And IsNumeric(Cells(r, 2).Value) Then Cells(r, 3).Value = Cells(r, 1).Value * Cells(r, 2).Value Cells(r, 3).NumberFormat = "0.00" End If Next r End SubThe IsNumeric check skips blank or text rows so the macro does not error out halfway down. - Save the workbook as macro-enabled. Use File > Save As and choose Excel Macro-Enabled Workbook (*.xlsm). A plain .xlsx file discards the code when saved.
- Add a button. On the sheet, go to Developer > Insert > Button (Form Control), drag a button onto the sheet, and assign it to your macro in the dialog that appears. Rename it something like "Calculate Percentage". Clicking the button is far friendlier than opening the editor each time.
- Test with known values. Try 200 with 15% (expect 30), 80 with 12.5% (expect 10) and a blank row (expect nothing, no error). If all three behave, the macro is sound.
Common Mistakes
- Multiplying by 100 too early. If B2 shows 20% but holds 0.2, multiplying by 100 again gives 2,000% of the number. Only divide or multiply by 100 when you are converting raw percentage figures into decimals or back.
- Storing percentages as text. A cell formatted as Text keeps "20%" as characters; multiplying text produces a #VALUE! error or zero. Format the input cell as Percentage or General first.
- Forgetting NumberFormat. The maths is right but the cell inherits General formatting, so 52.5% of 100 displays as 52.5 without a percent sign — which is correct, since the result is a plain number, not a percentage. Do not format the result as Percentage unless you want 52.5 to display as 5,250%.
- Saving as .xlsx. The code vanishes on save and the button stops working. Always keep the macro version as .xlsm.
- Hard-coding row numbers. A macro written for row 2 does nothing useful when new data arrives in row 30. Loop over a range or use the last used row.
- Disabling all macros permanently. It is tempting when the security bar appears, but it blocks your own workbook too. Use "Disable with notification" and click Enable Content only for files you trust.
Tips
- You probably do not need a macro at all. For a one-off calculation,
=A2*B2in a cell does the same job without code. Reach for VBA when you need prompts, buttons, batch processing or repeated work across many files. - Name your output range or use a table so formulas and macros reference structured columns rather than fixed addresses.
- Add
Option Explicitat the top of the module. It forces you to declare variables and catches typos likepctVallthat would otherwise silently return zero. - Keep a comment line above each Sub explaining what it does and which cells it touches. Future you will thank present you.
- Store shared macros in a personal macro workbook (Excel creates PERSONAL.XLSB automatically when you record a macro to that location) so they are available in every file.
Troubleshooting
- "Macros have been disabled" bar. Click Enable Content. If the file came from elsewhere and you do not trust it, do not enable — and never open macro files from unknown sources.
- #VALUE! or zero result. Check the source cells: click one and look at the formula bar. If it starts with an apostrophe or is left-aligned, it is text. Convert with the cell format dropdown or re-type the value.
- Compile error: Sub or Function not defined. The macro name you assigned to the button does not match the Sub in the module. Right-click the button > Assign Macro and pick the correct name.
- Macro runs but nothing changes. The active sheet is not the one you expect, or multiple sheets share the name. Qualify your references, for example
Worksheets("Sheet1").Range("C2"). - File opens read-only or the button is greyed out. Close the file, check it is not open elsewhere or marked read-only in the file properties, then reopen.
Frequently Asked Questions
Can this run in Excel for the web?
No. VBA macros run only in the desktop versions for Windows and Mac. In the browser version the code is not executed, so use a normal formula such as =A2*B2 there instead.
How do I make the result show as a percentage?
If you want the result to read as a percentage of the original, divide the part by the whole instead: result = base × percentage gives an amount, while part / whole formatted as Percentage gives a share. Set .NumberFormat = "0.0%" only when the output is genuinely a fraction.
Why does my macro give a number 100 times too big?
You are multiplying by a whole percentage (20) rather than a decimal (0.2). Either divide the percentage by 100 in code or store percentages as percentage-formatted cells.
Will the macro run on a colleague's computer?
Yes, if they open the .xlsm file in desktop Excel and enable macros. On Mac, some legacy references differ slightly, but a simple arithmetic macro like this one is portable.
How do I delete or edit the macro later?
Press Alt + F11 (Option + F11 on Mac), find the module in the project list, and edit or delete the code. If you delete the code, also reassign or remove any button pointing at it.