Excel VBA: Get or Set a Worksheet Cell Value
In this article
To read or write a worksheet cell value in Excel VBA, use the Range or Cells property of a worksheet object and assign it with =. For example, Range("A1").Value = 100 writes 100 to A1, and x = Range("A1").Value reads it into a variable. The steps below show the exact syntax, how to avoid the most common errors, and how to work with the active sheet, a named sheet, and the correct workbook.
What You'll Need
- Excel for Windows or Mac with the Developer tab enabled (File > Options > Customize Ribbon > check Developer, or Excel > Preferences > Ribbon & Toolbar on Mac).
- A workbook saved as .xlsm (macro-enabled) if you want to keep your VBA code.
- Basic familiarity with the VBA Editor (Alt+F11 on Windows, Option+F11 on Mac).
Step-by-Step
- Open the VBA Editor. Press Alt+F11 (Windows) or Option+F11 (Mac). In the Project Explorer, find your workbook and insert a module via Insert > Module. Code in a standard module is available to all sheets; code in a sheet module runs only for that sheet.
- Write a value to a cell. Type
Range("B2").Value = "Hello". This writes to the active sheet of the active workbook. To write a number, omit quotes:Range("B2").Value = 42. To write a date, useRange("B2").Value = Dateor a specific date like#1/15/2025#. - Read a value from a cell. Declare a variable and assign it:
Dim s As Stringthens = Range("B2").Value. UseVariantif the cell might hold numbers, text, or errors:Dim v As Variant: v = Range("B2").Value. Reading into a String converts numbers to text, which can change formatting. - Use Cells for row/column references.
Cells(2, 1).Valuerefers to row 2, column 1 (A2). This is useful in loops:For i = 1 To 10: Cells(i, 1).Value = i: Next ifills A1:A10 with 1 to 10.Cellsalways takes row first, column second. - Target a specific worksheet. Unqualified
RangeandCellsuse the active sheet, which can change. Qualify with the sheet name:Worksheets("Sheet1").Range("A1").Value = 5. Use the sheet's CodeName (visible in the Properties window, e.g.,Sheet1) for stability if the tab is renamed:Sheet1.Range("A1").Value = 5. - Target a specific workbook. If more than one workbook is open, qualify with the workbook:
Workbooks("Book1.xlsx").Worksheets("Sheet1").Range("A1").Value = 5. Without this, the code may write to the wrong file. - Run the code. Place the cursor inside the procedure and press F5, or use Run > Run Sub/UserForm. To run from the sheet, assign the macro to a button (Developer > Insert > Button) or use Alt+F8 to open the Macro dialog and choose Run.
- Check the result. Switch back to Excel (Alt+F11 toggles). If nothing changed, confirm the code ran on the intended sheet and that the cell is not protected. Protected sheets block writes unless the code unprotects first:
ActiveSheet.Unprotect "password"before writing, thenProtectafter.
Common Mistakes
- Omitting
.Value.Range("A1") = 5works because Value is the default property, but being explicit avoids confusion, especially withRange("A1").Formulaor.Text. - Using
.Textinstead of.Value..Textreturns the displayed string, so a column too narrow shows####and a formatted number may return text. Use.Valuefor the underlying value. - Forgetting to qualify the sheet. Unqualified references act on the active sheet. If the user clicks another tab, the code writes to the wrong place. Always name the sheet or use its CodeName.
- Assuming a number stays a number. Assigning to a String variable or a cell formatted as Text can store numbers as text. Use
Val()orCDbl()when reading, and setNumberFormatbefore writing if needed. - Writing to a merged cell. Only the top-left cell of a merged range accepts a value; writing to another part raises an error. Unmerge or target the top-left cell.
- Ignoring errors. Reading an empty cell returns
Empty, and reading an error cell returns an error value. Check withIsEmpty()orIsError()before using the result.
Tips
- Use
Withblocks to shorten repeated references:With Worksheets("Data"): .Range("A1").Value = 1: .Range("A2").Value = 2: End With. - Read and write whole ranges at once for speed:
Range("A1:A10").Value = 5fills all ten cells. Reading a range into a Variant array is much faster than looping cell by cell. - Use
ThisWorkbookto refer to the workbook containing the macro, which is safer thanActiveWorkbookwhen other files are open. - Turn off
ScreenUpdatingduring large writes and turn it back on at the end; this speeds up code and prevents flicker. - Use
Option Explicitat the top of the module to force variable declaration and catch typos early.
Troubleshooting
- "Subscript out of range" (Error 9): The sheet or workbook name does not exist. Check spelling and that the workbook is open. Use the exact tab name, including spaces.
- "Application-defined or object-defined error" (Error 1004): Often caused by writing to a protected sheet, an invalid range address, or a merged cell. Unprotect the sheet, verify the address, or target the top-left cell.
- Nothing happens: The macro may not be running, or it ran on a different sheet. Add
Debug.Print Worksheets("Sheet1").Range("A1").Valueand check the Immediate Window (Ctrl+G) to confirm the target. - Value appears as text: The cell is formatted as Text or the variable is a String. Set
Range("A1").NumberFormat = "General"before writing, or assign to a numeric variable. - Macro security blocks the code: Check Trust Center settings (File > Options > Trust Center > Trust Center Settings > Macro Settings). Enable macros for the file, or save it in a trusted location.
Frequently Asked Questions
How do I get the value of a cell in VBA?
Use Range("A1").Value or Cells(1, 1).Value and assign it to a variable, for example Dim v As Variant: v = Range("A1").Value.
What is the difference between Range and Cells?
Range uses an address like "A1" or "A1:B10"; Cells uses row and column numbers, which is easier in loops. Both refer to the same cells.
Can I write to a cell on another sheet?
Yes, qualify the sheet: Worksheets("Sheet2").Range("A1").Value = 10. The sheet must exist and not be protected.
Why does my code write to the wrong sheet?
Unqualified references use the active sheet. Always name the worksheet or use its CodeName to avoid this.
How do I read a cell value into a string?
Declare Dim s As String and assign s = CStr(Range("A1").Value). Use CStr to convert safely, but note that formatting is not preserved.
Can I set a cell value without selecting the sheet?
Yes, you never need to activate a sheet to read or write its cells. Qualify the sheet directly, which is faster and avoids screen flicker.