Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Percent of Number in Excel VBA: How-To Guide

· 8 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To calculate a percent of a number in Excel VBA you simply multiply the number by the percentage expressed as a decimal, then write the result to a cell and format it. For example, Range("B1").Value = Range("A1").Value * 0.2 puts 20% of the value in A1 into B1. Because VBA treats a percentage as a plain decimal, 20% is 0.2, not 20, and that single fact explains most errors people hit. Below is a complete walkthrough, from opening the editor to handling blank cells, whole columns and formatted output.

What You'll Need

  • A desktop copy of Excel (Windows or Mac). VBA does not run in Excel for the web or the mobile apps.
  • A workbook saved in the macro-enabled format (.xlsm) if you want the code to survive closing and reopening.
  • The Developer tab visible, or the ability to open the VBA editor with Alt+F11 on Windows or the equivalent menu on Mac (Tools > Macro > Visual Basic Editor).
  • Macro security settings that allow macros in your own file. If macros are disabled entirely, the code will not run at all.

Menu labels and macro security wording vary slightly between Excel versions, platforms and regions, so expect small differences from what you see here.

Step-by-Step

  1. Open the VBA editor. Press Alt+F11 on Windows. On Mac, use the Tools > Macro > Visual Basic Editor path from the Developer tab or the menu bar. A separate window opens with a Project pane on the left.
  2. Insert a module. In the editor, choose Insert > Module. A blank code window appears. Code in a standard module can be run from the Macros dialog; code in a sheet module only runs on events, which is a common source of confusion.
  3. Write the simplest version. Type a short procedure that reads one cell and writes the percentage result to another:

    Sub PercentOfNumber()
      Range("B1").Value = Range("A1").Value * 0.2
    End Sub

    This takes the value in A1 and writes 20% of it to B1. Keep a test value like 250 in A1 so you can check the answer is 50.
  4. Run it. Press F5 with the cursor inside the procedure, or go back to Excel and use Developer > Macros, select PercentOfNumber and click Run. Check cell B1. If B1 shows 50, the maths is right and you only need to deal with formatting.
  5. Apply the percentage or number format. A raw result of 0.2 in a cell looks like a decimal until you format it. Either write the percentage as a formatted string with Format(value * 100, "0.0") & "%", or keep the numeric value and set Range("B1").NumberFormat = "0.0%". Prefer the number format route when you will reuse the value in later formulas, because text cannot be summed.
  6. Use variables so you can change the rate easily. Declare a rate and read both cells into typed variables:

    Dim amount As Double, rate As Double
    rate = 0.075
    amount = Range("A1").Value
    Range("B1").Value = amount * rate

    Typing variables as Double avoids the silent rounding that comes from the default Variant type on very large numbers.
  7. Guard against blanks and text. A blank cell reads as zero, and a cell containing text causes a type mismatch error. Test first: If IsNumeric(Range("A1").Value) And Len(Range("A1").Value) > 0 Then before doing the multiplication, and handle the else case by writing an empty string or skipping the row.
  8. Loop over a whole column. For a list of amounts in A2 down to the last used row, find the last row with Cells(Rows.Count, 1).End(xlUp).Row, then loop For i = 2 To lastRow and write the result into column B for each row. This is the pattern to use for a discount column, a tax column or a commission column.
  9. Work through the worksheet rather than cell by cell where speed matters. For large ranges, read the range into a VBA array with Dim data As Variant: data = Range("A2:A10000").Value, calculate in memory, then write the array back to the sheet in one assignment. Writing cells one at a time inside a long loop is noticeably slow and can look like a freeze.
  10. Save as .xlsm and test. Use File > Save As and pick Excel Macro-Enabled Workbook. Close and reopen the file, then run the macro again to confirm it is still available and that macros are enabled for that location.
  11. Set the calculation mode if you use worksheet formulas. If instead of VBA arithmetic you write a formula such as Range("B1").Formula = "=A1*20%", remember that calculation may be set to manual. Check Formulas > Calculation Options and set it to Automatic if results look stale.

Common Mistakes

  • Using 20 instead of 0.2. The single most frequent error. VBA does not interpret a number as a percentage; multiplying by 20 gives twenty times the value, not twenty percent.
  • Expecting a percent sign from arithmetic. Multiplication returns a number. Only cell formatting or the Format function adds the display of a percent sign.
  • Assuming a formatted cell is a formatted value. If you build a text string such as "12.5%", Excel treats it as text, so SUM and other formulas ignore it. Keep numbers numeric and format the cell.
  • Forgetting the leading apostrophe problem. Values pasted from elsewhere can be stored as text. A cell that looks like a number but behaves like text will cause a type mismatch or calculate as zero.
  • Dividing when you should multiply. If you want 15% of a number, multiply by 0.15. If you want to know what percent one number is of another, divide and format as a percentage. Mixing the two produces answers that are wildly off.
  • Placing code in the wrong module. A procedure that never appears in the Macros list usually lives in a worksheet or workbook object module rather than a standard module.
  • Ignoring rounding. Percentages of money often need rounding to two decimals. Use Round(value, 2) when the result will be used in accounting, and be aware that banker's rounding can differ from what you expect at exact .5 values.

Tips

  • Store the rate in a single named cell, such as a settings cell on the sheet, and read it in the macro. A rate typed inside the code needs editing every time it changes.
  • Use Option Explicit at the top of the module. It forces every variable to be declared, which catches typos in names that would otherwise silently produce zero.
  • Keep a percent value as a decimal everywhere in the code and only format for display. Converting back and forth between 0.2 and 20 invites mistakes.
  • Give the output column a clear header and a number format before filling it, so the results are readable immediately.
  • Test with a small known set first: 100 and 10% should give 10, and 250 and 20% should give 50. If those are right, the loop is usually sound.
  • If a calculation must never be edited, place the logic in the macro and write only values to the cells, rather than leaving live formulas that someone can overwrite.

Troubleshooting

The result is 100 times too big. You used the whole number, such as 20, instead of the decimal 0.2. Divide your rate by 100 in the code.

The result is zero. The source cell is blank, contains text, or the macro is reading the wrong column. Check the address you pass to Range and confirm the cell contains a real number. Selecting the cell and looking at the formula bar shows whether a leading apostrophe is hiding a text value.

Run-time error 13, type mismatch. Text or an error value like #N/A is in the source range. Loop with an IsNumeric check and skip or flag bad rows rather than stopping the whole run.

Run-time error 1004. Usually a reference to a sheet or range that does not exist on the active workbook, or a protected sheet. Unprotect the sheet in Excel, or make sure the macro refers to the correct worksheet by name.

The macro is not in the list. It is in the wrong module, it is declared Private, or it takes arguments. Move it to a standard module and make it a plain Sub with no parameters.

Nothing happens at all, and a security bar appears. Macros are disabled. Enable macro content for that file through the Developer tab's macro security settings, or save the workbook in a trusted location. Do not disable macro security globally just to make one file run.

The numbers look stale after editing. Calculation is set to manual. Set it back to automatic under Formulas > Calculation Options. If that did not work, close and reopen the workbook before changing anything else.

Formatting is lost after the macro runs. Writing a value does not clear a number format, but writing a formatted text string into a cell that already had a number format does. Check that you are writing a number and applying the format separately.

Frequently Asked Questions

How do I calculate 15% of a number in VBA?

Multiply by 0.15. For example, Range("B1").Value = Range("A1").Value * 0.15. If A1 holds 200, B1 shows 30 before formatting.

Why does VBA give a hundred times the answer?

Because you multiplied by the whole percentage number rather than its decimal form. Use 0.2 for 20%, or divide the rate by 100 first.

Can I show the result with a percent sign automatically?

Yes. Keep the value numeric and set the cell's number format to something like 0.0%, or build text with Format(value * 100, "0.0") & "%" when you do not need to do further maths on it.

How do I apply one percentage to a whole column?

Find the last used row, loop from the first data row to that row, and write Cells(i, 2).Value = Cells(i, 1).Value * rate inside the loop. For very large ranges, read the cells into an array and write back in one go.

What happens if a cell is blank or contains text?

A blank cell is treated as zero, so the result is zero. Text causes a type mismatch error, so test with IsNumeric before calculating.

Can I use this in Excel for the web or on a phone?

No. VBA macros are not supported in Excel for the web or the mobile apps. They run in the desktop versions of Excel for Windows and Mac.