You put a value in a cell in Excel VBA by assigning to the cell's .Value property, like this: Range("A1").Value = "Hello" or Cells(1, 1).Value = 42. That single line of code writes text, a number, a date or even a formula into the specified cell when the macro runs. Everything below builds on that one pattern: how to target the right cell, how to write from loops, how to avoid overwriting data by accident, and what to do when your value lands in the wrong place or does not land at all.
What You'll Need
- Excel on Windows or Mac with macros enabled (the Developer tab visible, or the ability to open the VBA editor with Alt+F11 on Windows or Option+F11 on Mac)
- A workbook saved as .xlsm (macro-enabled) if you want the code to survive closing and reopening the file
- Basic familiarity with the VBA editor: inserting a module and pressing F5 to run
- Permission to run macros on your machine; some corporate setups block them by policy, in which case you cannot run the code at all
Step-by-Step
- Open the VBA editor. Press Alt+F11 on Windows or Option+F11 on Mac. Ribbon labels and keyboard shortcuts can differ slightly by Excel version, platform and region, but the editor itself is the same tool.
- Insert a standard module. In the editor menu choose Insert > Module. Code typed into a sheet's own code module (the ones named like Sheet1) runs only on that sheet's events, so a normal module is easier to test.
- Write the simplest assignment. Type a short procedure:
Sub WriteOneCell() Range("A1").Value = "Hello" End SubThis puts the text Hello in cell A1 of the active sheet. - Run it. Click inside the procedure and press F5, or use Run > Run Sub/UserForm. Switch back to Excel with Alt+F11 and check A1.
- Try the Cells version.
Cells(1, 1).Value = 42writes the number 42 to row 1, column 1 (also A1). Cells takes row first, column second, which makes it the natural choice inside loops. - Write to a specific sheet, not whichever is active. Qualify everything:
Sub WriteQualified() Worksheets("Data").Range("B2").Value = 100 End SubWithout theWorksheets("Data")prefix, the code writes to whichever sheet happens to be active when it runs, which is the number one cause of values appearing in the wrong place. - Use a named range where you have one. If the cell is part of a named range called TaxRate,
Range("TaxRate").Value = 0.2is readable and survives rows being inserted above it. - Write dates and formulas correctly. For a date use
Range("A2").Value = DateSerial(2026, 12, 4)so regional date formats cannot flip the month and day. For a formula, assign the formula string:Range("C1").Formula = "=SUM(A1:B1)". Assigning a string that starts with = to.Valuealso produces a formula in most cases, but.Formulastates the intent. - Write values in a loop. To fill ten cells in column A:
Sub FillColumn() Dim i As Long For i = 1 To 10 Worksheets("Data").Cells(i, 1).Value = i * 10 Next i End Sub - Write a whole range at once. For larger blocks, an array is much faster than a cell-by-cell loop:
Sub FillBlock() Dim v(1 To 3, 1 To 2) As Variant v(1, 1) = "Name": v(1, 2) = "Score" v(2, 1) = "Ana": v(2, 2) = 91 v(3, 1) = "Ben": v(3, 2) = 78 Range("A1:B3").Value = v End SubThe array dimensions must match the range exactly or the assignment fails. - Save as .xlsm. Use File > Save As and pick the macro-enabled workbook type. A plain .xlsx will strip the code out when you save.
Common Mistakes
- Missing sheet qualification. Writing
Range("A1").Value = xinside a loop that activates other sheets sends values to the wrong sheet. Always name the worksheet. - Confusing Rows and Columns in Cells.
Cells(2, 5)is row 2, column E. Reversing them silently writes to a different cell with no error. - Writing past the last row. In older .xls workbooks the sheet holds 65,536 rows; in .xlsx files it holds 1,048,576. Writing to row 1,048,577 raises an error, and a loop that increments past the limit will crash midway.
- Turning numbers into text. Assigning a value that looks numeric but comes from a text source (for example a cell formatted as Text) leaves it stored as text, so SUM ignores it. Test with
IsNumericor convert explicitly withCLngorCDbl. - Overwriting without checking. Assignment replaces whatever is in the cell. If the cell holds a formula, the formula is gone for good once the macro runs. Check with
If IsEmpty(...) Thenor work on a copy first. - Assigning to
.Textor.Formulaby accident..Textis read-only for practical purposes and reflects what is displayed, so writing to it fails. - Using a protected sheet. Writing to a locked cell on a protected sheet raises an error unless the code unprotects with the correct password first.
Tips
- Turn off screen updating during big writes with
Application.ScreenUpdating = Falseat the start and set it back to True at the end. It makes loops noticeably faster. - Use
Withblocks when writing several cells on one sheet:With Worksheets("Data")then.Range("A1").Value = 1,.Range("A2").Value = 2,End With. - Store the target in a variable first:
Dim ws As Worksheet: Set ws = Worksheets("Data"). It is shorter and easier to change later. - To clear a cell instead of writing, use
Range("A1").ClearContents(keeps formatting) rather than assigning an empty string, which can leave the cell looking blank but still counted as used. - Write to the next free row with
Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Value = newValuefor simple append-style logging. - Comment each write step while testing. When a value appears one row off, the comment showing the intended target makes the fix obvious.
Troubleshooting
The macro runs but nothing changes. Check that the code actually targeted the sheet you are looking at; the value may have gone to another open workbook's active sheet. Close other workbooks during testing, or fully qualify every reference.
You get a "Subscript out of range" error. This usually means the worksheet name in Worksheets("Data") does not exist verbatim, including trailing spaces. Rename the sheet or correct the string.
You get a "Method 'Range' of object failed" or "Object required" error. The unqualified Range or Cells was used when no sheet was active (for example immediately after opening a workbook), or a variable was not set with Set.
Values look right but formulas elsewhere ignore them. The cells are probably stored as text. Select them and use Data > Text to Columns, Finish, or convert in code with CLng/CDbl.
Permission denied or the cell is read-only. The sheet is protected. Unprotect it before writing, then reprotect after, and make sure you are entitled to change it.
Error 1004 on large writes. Often a range or array size mismatch, or a write past the row limit. Verify array dimensions equal the range dimensions and that your row counter cannot exceed the sheet limit.
The code disappeared after saving. The workbook was saved as .xlsx. Save As > Excel Macro-Enabled Workbook (.xlsm) and re-add the code if it was already stripped.
If none of that helps, step through the procedure with F8 instead of running it whole. Watching each line execute and inspecting values in the Locals window shows exactly which write goes wrong and what the target cell is at that moment.
Frequently Asked Questions
What is the difference between Range and Cells?
Range takes a name or address string such as "A1" or "B2:D10". Cells takes a row number and column number, so it suits loops and calculated positions. Both write through the same .Value property.
Can I write a value to a cell without selecting it?
Yes. Assignment to .Value never needs Select or Activate. Selecting cells just slows the macro down.
How do I put a value in the last empty row?
Use Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Value. It starts at the bottom of column 1, jumps up to the last used cell, then moves down one row.
Why does my number show as text after the macro runs?
The destination cell is likely formatted as Text before the write, or you assigned a string. Format the cell as General or Number, or convert the value before assigning it.
Does writing to a cell erase its formatting?
No. Assigning .Value replaces the content only. Formatting, borders and fills stay. Clear removes both.
Can a macro write to a closed workbook?
Not directly. The workbook must be open, or your code must open it first, write, and then close it while saving.