The Excel VBA Cell object (more precisely, a Range object representing one cell) is the handle you use to read a value, write a value, change formatting, or act on a single cell from code. You get one by naming the cell's address against a worksheet (for example Worksheets("Sheet1").Range("A1")) or by using Cells(row, column). If you have never written a macro, the shortest working set of steps is: enable the Developer tab, open the Visual Basic Editor with Alt+F11, insert a module, type a two-line macro such as Range("A1").Value = "Hello", then run it with F5. The rest of this guide explains the object properly, including the mistakes that cause most of the errors.
What You'll Need
- A desktop copy of Excel for Windows or Mac. The Visual Basic Editor is not available in Excel for the web, and macro recording and editing in the mobile apps is extremely limited or absent, so do this work on a computer.
- A workbook saved as .xlsm (macro-enabled) if you want to keep the code. Standard .xlsx files discard macros when you save.
- The Developer tab visible. If it is missing, go to File > Options > Customize Ribbon (Excel Options > Ribbon & Toolbar on Mac) and tick Developer in the right-hand list. Labels can differ slightly by version and platform.
- Macro security set to allow your code to run. Under Developer > Macro Security, choose Disable all macros with notification so you get a prompt each time rather than silent blocking.
- A test workbook. Do your experimenting somewhere you do not mind changing, not in a live file.
Step-by-Step
- Open the editor. Press Alt+F11 on Windows (Fn+Option+F11 on many Macs), or click Developer > Visual Basic. A separate window opens with a Project Explorer on the left listing your open workbooks.
- Insert a module. In the editor choose Insert > Module. Code typed into a module is a normal macro that appears in the macro list. Code in a sheet module also works, but it is tied to that sheet and is easier to lose track of.
- Reference a cell by address. Type
Sub FirstCell(), press Enter, then typeWorksheets("Sheet1").Range("A1").Value = "Hello"and close withEnd Sub. TheValueproperty is what you read and write; the cell's contents behave like a variable. - Name the sheet explicitly. Always put the worksheet before the range. A bare
Range("A1")acts on whatever sheet is currently active, so the macro writes to a different place depending on what the user clicked. Explicit sheet names make the result predictable. - Use Cells when you need numbers.
Cells(3, 2)means row 3, column 2, which is B3. The order is row first, column second, and this is the form to use inside loops where the row number changes:Cells(i, 1).Value = i. - Combine the two for a reliable range.
Worksheets("Sheet1").Range("A1:C10")covers a block, andRange(Cells(1,1), Cells(10,3))builds the same block from numbers. Both are valid; pick whichever reads more clearly. - Read a value into a variable. Declare a variable first, for example
Dim total As Double, thentotal = Worksheets("Sheet1").Range("B2").Value. Writing to a Variant or a specific type lets you do arithmetic and comparisons on the contents. - Change formatting through the same object. Properties hang off the cell:
.Font.Bold = True,.Interior.Color = RGB(255, 255, 0),.NumberFormat = "0.00",.HorizontalAlignment = xlCenter. These change the cell itself, not just the value. - Use Offset and Resize for movement.
Range("B2").Offset(1, 0)is the cell one row down (B3).Range("B2").Resize(3, 2)stretches the reference to three rows and two columns. Both are useful in loops and both count from zero, so Offset(0,0) is the same cell. - Check before you write. Use
If IsEmpty(Range("A1").Value) Thenor testLen(Range("A1").Value) = 0before overwriting. A macro that writes over an existing entry with no warning is the most common way people lose data. - Run and inspect. Press F5 with the cursor inside the macro, or use Developer > Macros and pick the name. Add
Debug.Print Range("A1").Address & " = " & Range("A1").Valueto send the result to the Immediate window (View > Immediate Window, Ctrl+G) instead of changing the sheet. - Save as macro-enabled. Use File > Save As and choose Excel Macro-Enabled Workbook (.xlsm). Reopen the file to confirm the code survived and that the security prompt behaves as expected.
Common Mistakes
- Leaving off the worksheet. The single biggest source of wrong-cell writes, because
RangeandCellsunqualified follow the active sheet. - Reversing Cells arguments.
Cells(2, 3)is C2, not B3. Row comes first. - Confusing Value with Text.
.Valuereturns the underlying value, so a date may come back as a serial number;.Textreturns what is displayed but only works on a single cell, not a multi-cell range. - Writing to a protected sheet. The macro fails with a permission error. Unprotect first, or write only to unlocked cells.
- Using a cell address from the wrong sheet. Sheet references are not translated, so a copied line still points at the original sheet.
- Assuming an empty-looking cell is empty. A cell holding a space or a formula returning "" is not blank; test with the right check.
- Saving as .xlsx. Excel warns you, but clicking through on autopilot produces a file with no macros.
Tips
- Turn on Option Explicit at the top of the module (Tools > Options > Require Variable Declaration) so misspelled variable names are caught immediately.
- Use named ranges for cells the macro must find again after users insert rows; names move with their target, hard addresses do not.
Range("A1").CurrentRegionselects the block around a cell, which is handy for processing a whole table without guessing its size.- Turn off
Application.ScreenUpdatingat the start and back on before exit for long loops; it makes big writes dramatically faster. - Avoid writing cell by cell when you can read a whole range into an array, process it, and write it back in one statement.
- Comment each macro with what sheet and cells it touches. Future you will not remember.
Troubleshooting
If nothing happens when you press F5, check the cursor is inside the procedure and that the macro is in a module, not in a sheet or ThisWorkbook object with a different scope.
If you get a "Subscript out of range" error, the worksheet name in your code does not match the tab name exactly, including spaces. If you get "Object required" or "Method 'Range' of object failed", the sheet reference is wrong or the workbook is closed.
If the macro writes to the wrong place, look for an unqualified Range or Cells and add the worksheet prefix. If formatting disappears, remember that a value write does not change formatting, but a delete or a Clear does, so use ClearContents when you want to keep formats.
If the code runs once and then errors, you may have created a file with a name that is already open, or the workbook reference was lost after you closed and reopened the file. Reopen the file, confirm the module still contains the code, and check the sheet name has not been renamed. If macros do not run at all, check the Trust Center macro settings and whether the file is stored in a trusted location.
Frequently Asked Questions
What is the difference between Range and Cells?
Range takes an address like "A1" or "A1:C10"; Cells takes a row number and a column number. Cells is convenient in loops, Range is easier to read for fixed addresses. Both return the same kind of object.
How do I reference a cell on another sheet?
Put the worksheet before the range: Worksheets("Data").Range("A1"). For a closed workbook, open it first or use the workbook's name in the expression.
Can I select a cell without changing what the user has selected?
Yes, usually you never need to select at all. Write directly to the range's value or formatting; Select and Activate only change the visible selection and are slower.
Why does my date come back as a number?
Dates are stored as serial numbers. Read them as a Variant or Date variable, or format the target cell with a date NumberFormat.
Does this work in Excel for the web?
No. The Visual Basic Editor and VBA macros are not available there. Use the desktop app for VBA work.
How do I refer to the last used row?
Cells(Rows.Count, 1).End(xlUp).Row gives the last filled row in column A, which avoids hard coding a row number.