Insert Formula in Cell VBA: Step-by-Step Guide
In this article
To insert a formula into a cell with VBA, set the cell's Formula property to the formula string, including the leading equals sign: Range("A1").Formula = "=SUM(B1:B10)". That single line writes a live formula, not text, and Excel calculates it immediately. The same approach works for FormulaR1C1 if you prefer relative references, and for .Value when you want the result rather than the formula. This guide walks through the exact syntax, the difference between the properties, and the fixes for the errors people hit most often.
What You'll Need
- Excel with the Developer tab enabled (File > Options > Customize Ribbon > check Developer), or any Office app that hosts VBA such as Word or Access.
- A workbook saved as
.xlsmif you want the macro to persist;.xlsxstrips VBA on save. - Basic familiarity with the Visual Basic Editor (Alt+F11 opens it).
- Knowledge of the formula you want to insert, written in English function names and with commas or semicolons as your regional settings require.
Step-by-Step
- Open the Visual Basic Editor. Press Alt+F11 (on Mac, Fn+Alt+F11 or the Developer tab > Visual Basic). The editor opens in its own window.
- Insert a module. In the editor, choose Insert > Module. A blank code pane appears. If you want the code to run on a specific sheet event, insert it into that sheet's code module instead, but a standard module is the right place for a general-purpose macro.
- Start the subroutine. Type
Sub InsertFormula()and press Enter. The editor addsEnd Subautomatically. - Qualify the worksheet. Reference the sheet explicitly so the code does not depend on which sheet is active:
Dim ws As WorksheetthenSet ws = ThisWorkbook.Worksheets("Sheet1"). ReplaceSheet1with the tab name exactly as shown, including spaces. - Write the formula to the target cell. Add
ws.Range("A1").Formula = "=SUM(B1:B10)". The string must begin with=; without it Excel stores plain text. Use doubled quotes inside the string if the formula itself contains quotes, for example"=IF(C1>0,""Yes"",""No"")". - Run the macro. Press F5 with the cursor inside the subroutine, or return to Excel and use Developer > Macros, select
InsertFormula, and click Run. Cell A1 now holds a working formula. - Choose the right property for your goal. Use
.Formulafor A1-style references,.FormulaR1C1for relative references that stay consistent when copied, and.Valueif you want the calculated result frozen rather than a live formula. For example,ws.Range("A1").FormulaR1C1 = "=SUM(RC[1]:RC[10])"sums the ten cells to the right of A1. - Fill a whole range at once if needed. Assign the formula to a multi-cell range and Excel adjusts relative references for each cell:
ws.Range("C2:C100").Formula = "=A2+B2"puts the correct row-specific formula in every cell. - Save as a macro-enabled workbook. Use File > Save As and pick Excel Macro-Enabled Workbook (
.xlsm). Saving as.xlsxdiscards the code.
Common Mistakes
- Omitting the leading equals sign.
.Formula = "SUM(B1:B10)"writes text, not a formula. Always start the string with=. - Using the wrong quote style. VBA strings need straight double quotes. Smart quotes pasted from a document cause a compile error.
- Forgetting to double internal quotes. A formula containing text such as
"Yes"must be written as""Yes""inside the VBA string. - Assuming the active sheet. Unqualified
Range("A1")targets whichever sheet is active, which may not be the one you intended. Qualify with a worksheet object. - Mixing A1 and R1C1 syntax.
.Formulaexpects A1 references;.FormulaR1C1expects R1C1 references. Mixing them produces wrong results or errors. - Regional list separators. In locales where the list separator is a semicolon, formulas may need
=SUM(B1;B10)when written through.Formula..FormulaR1C1with English function names is generally more portable. - Saving in the wrong format. A macro-enabled workbook saved as
.xlsxloses its code silently.
Tips
- Use
.Formula2instead of.Formulafor dynamic array formulas in modern Excel;.Formulacan raise an error for formulas that spill. - Test the formula manually in a cell first, then copy the exact string into VBA. This avoids debugging syntax inside the editor.
- Turn off
Application.ScreenUpdatingwhile writing many formulas, then restore it, to speed up large loops. - Use
ws.Cells(row, column).Formulainside loops so row and column numbers can be calculated rather than hard-coded. - If you want the formula converted to its result, write the formula first, then read
.Valueand assign it back to the same cell.
Troubleshooting
If the cell shows the formula as text, check that the string begins with = and that the cell is not formatted as Text before the write. Reformat the cell to General and run the macro again.
If you get a compile error on the line, look for unescaped quotes or a missing ampersand when concatenating strings. Break long formulas into a variable built with & so the editor can point to the exact problem.
If you get a runtime error 1004, the most common causes are a misspelled sheet name, a range that does not exist, or a formula that Excel cannot parse in the current locale. Verify the sheet tab name character by character and test the formula manually.
If the formula calculates the wrong cells, confirm whether you used A1 or R1C1 references and whether relative references shifted as expected when filling a range.
If nothing happens when you run the macro, make sure macros are enabled in the Trust Center and that you are running the correct subroutine from the Macros dialog.
Frequently Asked Questions
What is the difference between .Formula and .Value?
.Formula stores a live formula that recalculates; .Value stores the result, so the cell no longer updates when its inputs change.
Can I insert a formula that references another sheet?
Yes. Include the sheet name in the formula string, for example "=Sheet2!A1*2". Wrap sheet names containing spaces in single quotes inside the string, such as "='Sales Data'!A1".
Why does my formula appear as text after running the macro?
The string is missing the leading equals sign, or the target cell was formatted as Text. Fix the string and reset the cell format to General.
How do I insert the same formula into many cells?
Assign it to a multi-cell range such as ws.Range("C2:C100").Formula = "=A2+B2"; Excel adjusts relative references for each row automatically.
Does this work in Excel for Mac?
Yes, VBA is available in Excel for Mac with the same .Formula property, though the keyboard shortcut to open the editor differs and some Windows-only features are unavailable.
Can I use a variable inside the formula string?
Yes. Concatenate with &, for example ws.Range("A1").Formula = "=B1*" & factor, where factor is a numeric variable.