Excel VBA Range.Value: Read and Write Cells
In this article
You read or write a cell's contents in Excel VBA with the Value property of a Range object — for example Range("A1").Value = 42 writes 42 into A1, and x = Range("A1").Value reads it back. That single property is the workhorse of almost every macro that moves data between cells, worksheets or workbooks. The steps below show the exact syntax, how to handle multi-cell ranges safely, and the mistakes that cause the most confusing errors.
What You'll Need
- Excel on Windows or Mac with the Developer tab enabled (File > Options > Customize Ribbon > tick Developer, or Excel > Preferences > Ribbon & Toolbar on Mac).
- A workbook saved as .xlsm if you want the code to persist; a normal .xlsx will strip macros on save.
- Basic familiarity with the Visual Basic Editor (Alt+F11 on Windows, Option+F11 on Mac).
Step-by-Step
- Open the editor and insert a module. Press Alt+F11 (Option+F11 on Mac), then in the Project pane right-click your workbook and choose Insert > Module. Code in a standard module can be run from anywhere; code in a sheet module only fires on that sheet's events.
- Write a value into a single cell. Type
Range("A1").Value = "Hello". The equals sign assigns to the cell, not to a variable. Numbers, dates, text and booleans all work:Range("B2").Value = 100orRange("B2").Value = Date. - Read a value out of a cell. Declare a variable of a sensible type and assign the property to it:
Dim s As Stringthens = Range("A1").Value. For numbers useDoubleorLong; for dates useDate. Reading into aVariantis safest when the cell might be empty. - Point at a specific sheet. Unqualified
Rangerefers to the active sheet, which is why macros mysteriously write to the wrong place. Qualify it:Worksheets("Data").Range("A1").Value = 5. Use the sheet's tab name exactly as shown, including spaces inside the quotes. - Work with a block of cells.
Range("A1:C3").Valuereturns a two-dimensional Variant array (1-based, rows then columns). To read it:Dim v As Variantthenv = Range("A1:C3").Value, and access a cell withv(2, 3)for row 2, column 3. To write a block, assign an array of matching dimensions back to.Value. - Use Value2 for dates and currency.
.Value2behaves like.Valuebut returns dates as serial numbers and currency without the currency data type. It is faster and avoids type surprises when you only need the underlying number. - Copy between ranges efficiently. For a like-for-like copy,
Range("A1:A10").Value = Range("B1:B10").Valueworks and is much faster than looping cell by cell. Both ranges must be the same size or you get a mismatch error. - Clear a cell properly. Set
Range("A1").Value = Emptyor useRange("A1").ClearContents. Assigning an empty string""leaves a text value thatISBLANKwill not treat as blank. - Run and check. Press F5 with the cursor inside the routine, or assign the macro to a button via Developer > Insert. Step through with F8 to watch each assignment land in the sheet.
Common Mistakes
- Forgetting the sheet qualifier. The single biggest cause of "it wrote to the wrong sheet". Always name the worksheet unless you genuinely mean the active one.
- Using
Setfor a value.Set Range("A1").Value = 5is invalid;Setis only for object references. Drop it for simple values. - Confusing
ValuewithText..Textreturns exactly what is displayed, including formatting such as "1,234.00" or "###" when the column is too narrow. Use.Valuefor the real underlying content. - Assuming a multi-cell read is one-dimensional. It is always a 2D array, even for a single column, so
v(1)fails; usev(1, 1). - Leaving implicit references. Inside a
Withblock, a bare.Valueis fine, but a bareRangeinside that block still points at the active sheet, not theWithtarget. - Writing text that looks like a number. Assigning
"007"stores text; assign7if you need a number, and format the cell to show leading zeros.
Tips
- Turn off
Application.ScreenUpdating = Falsebefore large loops and back on afterwards; range writes get dramatically faster. - Read a whole block into a Variant array, process it in memory, then write it back in one assignment. This avoids thousands of individual cell writes.
- Use
IsEmpty(Range("A1").Value)to test for a truly blank cell rather than comparing to"". - Prefer named ranges (
Range("TaxRate").Value) in code that others will maintain; renaming a range then updates every reference. - Remember that
.Valueon a merged range only reliably reads or writes the top-left cell; unmerge if you need per-cell control.
Troubleshooting
If you get Run-time error 1004: Application-defined or object-defined error, the range reference is usually wrong — a misspelled sheet name, a range on a sheet that is not active, or a name that does not exist. Check the sheet tab spelling first.
If Run-time error 13: Type mismatch appears, you are assigning a value into a variable type that cannot hold it — for example reading text into a Long. Declare the variable as Variant while debugging, then tighten it once the data is known.
If Run-time error 9: Subscript out of range appears, the worksheet name in Worksheets("...") does not match any tab. Rename the tab or fix the string, and remember names are case-insensitive but spaces matter.
If the macro runs but nothing changes, confirm you are looking at the sheet the code actually wrote to, and that the workbook is not protected. A protected sheet silently rejects writes and raises an error only if you have not handled it.
If values appear as text or dates turn into numbers, switch between .Value and .Value2, and set the cell's NumberFormat explicitly after writing.
Frequently Asked Questions
What is the difference between Range.Value and Range.Value2?
Value2 returns dates as serial numbers and currency as plain numbers, which is faster and avoids the currency data type. Use Value when you want dates returned as dates.
How do I read a whole column into VBA?
Use Dim v As Variant: v = Range("A1:A100").Value, then loop v(i, 1). Avoid entire-column references like A:A in code; they are slow and can pull in a million rows.
Can I write an array to a range in one line?
Yes, provided the array dimensions match the target range exactly. Assign Range("A1:C3").Value = myArray where myArray is a 3-by-3 Variant array.
Why does my macro write to the wrong sheet?
Because an unqualified Range always refers to the active sheet. Qualify every reference with Worksheets("Name") or use a With block.
How do I make a cell truly blank from VBA?
Use Range("A1").ClearContents or assign Empty. Assigning "" leaves a zero-length text value that is not blank to Excel functions.