To enter a formula into a cell using VBA, use the Range.Formula property. For example, Range("B2").Formula = "=A2*2" inserts the formula into cell B2. This is the most direct method, and it works for any formula that Excel accepts when typed manually.
Why Use VBA to Enter Formulas?
VBA is useful when you need to add formulas to many cells, update them dynamically, or create a template that recalculates automatically. Instead of typing formulas by hand, a macro can populate entire columns or worksheets in seconds. It also reduces human error and ensures consistency across workbooks. For example, a monthly report can use VBA to insert the same SUM or IF formula into dozens of sheets without manual copying.
The Core Properties: Formula, FormulaR1C1, and FormulaLocal
Excel VBA offers three main ways to assign a formula to a cell:
- Formula – Uses A1-style notation (e.g.,
"=A1+B1"). This is the most common and readable. - FormulaR1C1 – Uses R1C1 notation (e.g.,
"=RC[-1]+RC[1]"). This is useful when you want to write formulas relative to the target cell, especially in loops. - FormulaLocal – Uses the local language function names and separators. Rarely needed unless you work with non-English Excel versions.
For most tasks, Formula is sufficient. However, if you are filling a range with a pattern, FormulaR1C1 can be more efficient because it avoids recalculating absolute references.
Basic Examples: Entering a Formula in a Single Cell
Assume you have numbers in A1 and B1. To put a SUM formula in C1, use:
Sub EnterSumFormula()
Range("C1").Formula = "=SUM(A1:B1)"
End Sub
If you need to enter a formula with a string value, use double quotes inside the formula. For example, to concatenate text with a cell reference:
Range("D1").Formula = "=A1 & \" - \" & B1"
In VBA, a double quote inside a string is escaped by doubling it. So \" becomes " in the final formula.
Entering Formulas in a Range or Loop
To fill a range with the same formula, assign it to the entire range at once:
Range("C1:C10").Formula = "=A1*B1"
Excel automatically adjusts relative references for each row, so C2 gets =A2*B2, and so on. If you need to enter different formulas per cell, use a loop:
Sub FillWithLoop()
Dim i As Long
For i = 1 To 10
Cells(i, 3).Formula = "=A" & i & "*B" & i
Next i
End Sub
This gives the same result as the range assignment but is more flexible when you need to vary the formula based on conditions.
Using FormulaR1C1 for Relative References
R1C1 notation is powerful when you want to write a formula relative to the cell you are filling. For example, to put in C1 a formula that adds the two cells to its left:
Range("C1").FormulaR1C1 = "=RC[-2]+RC[-1]"
Here, RC[-2] refers to two columns to the left (A), and RC[-1] to one column left (B). If you apply this to a range, each cell gets the same relative pattern:
Range("C1:C10").FormulaR1C1 = "=RC[-2]+RC[-1]"
This is often used in loops that move across columns or rows, because you don’t have to build the A1 reference manually.
Common Pitfalls and How to Avoid Them
- Using the wrong property –
Valuestores a constant, not a formula. If you writeRange("A1").Value = "=B1+C1", Excel treats it as text, not a formula. Always useFormula. - Locale issues – If your Excel uses a comma as decimal separator, the
Formulaproperty still expects a period. UseFormulaLocalif you need local syntax, but it’s safer to stick withFormula. - Quotes inside formulas – Remember to double every quote that appears inside the formula string. For example,
"=IF(A1>0,\"Yes\",\"No\")". - Referencing sheets – If you are not on the active sheet, qualify the range, e.g.,
Worksheets("Sheet1").Range("A1").Formula = .... - Formula length – Excel has a limit of 8192 characters per formula. VBA will throw an error if you exceed that.
Entering Array Formulas and Dynamic Arrays
For legacy array formulas (entered with Ctrl+Shift+Enter), use FormulaArray:
Range("C1").FormulaArray = "=SUM(A1:A10*B1:B10)"
In modern Excel (Microsoft 365), dynamic array formulas like FILTER or SORT can be entered with the normal Formula property, and they spill automatically. For example:
Range("D1").Formula = "=FILTER(A2:A100,B2:B100>10)"
This will spill results down the column automatically. No special property is needed.
Frequently Asked Questions
How do I enter a formula that contains a variable?
Concatenate the variable into the formula string. For example, if you have a variable myRow set to 5, use Range("C1").Formula = "=A" & myRow & "*B" & myRow. The result will be =A5*B5.
Why does my formula show as text instead of calculating?
This usually happens when you use .Value instead of .Formula, or when the cell is formatted as Text. Change the cell format to General before assigning the formula, or use .Formula.
Can I enter a formula in a cell without activating the sheet?
Yes, fully qualify the range with the worksheet object. For example, ThisWorkbook.Worksheets("Data").Range("A1").Formula = "=1+1" works even if that sheet is not active.
How do I enter a formula that uses a named range?
Just use the name in the formula string, e.g., Range("B2").Formula = "=SUM(Sales)". Named ranges work exactly as they do in a regular worksheet formula.
Is there a way to enter a formula in every cell of a column at once?
Yes, assign the formula to the entire range, like Range("C1:C100").Formula = "=A1*B1". Excel fills the range with the formula, adjusting relative references for each row.
Safety and Best Practices
Before running any macro that writes formulas, save a copy of your workbook. Macros cannot be undone with Ctrl+Z. Also, if your formula references other sheets, ensure those sheets exist and have the correct names, or you’ll get a runtime error. When using loops, declare variables with Dim and consider using Application.ScreenUpdating = False to speed up the macro, then reset it to True at the end. Always test on a small range first.
Related
- Random Number Generation in JavaScript Explained
- Data Entry Operator Information & Job Facts
- JavaScript Get Random Number: Math.random() Explained
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Wow Macro for Turning in Quest?The fastest way to turn in a quest in World of Warcraft is to use the default interaction…
- VBA Copy and Paste a File: Practical Code and Safe StepsTo copy a file with VBA in Excel for Microsoft 365 on Windows, use the FileCopy statement or…
- VBA Send Email From a Shared Mailbox in OutlookYes. In classic Outlook for Windows, VBA can create a message and set its SentOnBehalfOfName property to the…
- Excel VBA: Find the First Empty Cell in a ColumnTo find the first empty cell in an Excel column with VBA, start at the first row you…
- Excel VBA Protect Sheet Allow Edit ObjectsYes, you can use Excel VBA to protect a worksheet while still allowing users to edit objects such…
- Excel Macro Location: Where VBA Code Is StoredIn Excel for Microsoft 365 on Windows, a macro is usually stored as VBA code inside the workbook…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.