Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel VBA Selection Offset: How to Shift Cells in Code

· 7 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

The Offset property moves a reference a set number of rows and columns away from a starting cell or range, and when that starting point is Selection you get Selection.Offset(row, column) - a way to have a macro act on the cell next to whatever the user has highlighted. The pattern is Selection.Offset(0, 1).Value = "Done", which writes "Done" into the cell one column to the right of the active selection. There is nothing to install: you write it in the Visual Basic Editor that ships with Excel, but the macro file must be saved as .xlsm or .xlsb or the code is lost when you close the workbook.

What You'll Need

  • Excel on Windows or Mac with macros enabled. The ribbon tab is called Developer; if it is missing, right-click an empty part of the ribbon, choose Customize the Ribbon, and tick Developer in the list on the right (labels can differ slightly by version and platform).
  • A workbook saved in a macro-enabled format (.xlsm). An ordinary .xlsx will not keep the code.
  • Macro security set to allow your own code. In the Visual Basic Editor, Tools > Options > General lets you confirm settings; on Windows the Trust Center (File > Options > Trust Center > Trust Center Settings > Macro Settings) is where the broader permission lives.
  • A sheet with some sample data - two or three rows of labels and values are enough to see the offsets work.

Step-by-Step

  1. Create the module. Press Alt+F11 (on Mac, Fn+Option+F11 or the Developer > Visual Basic button) to open the Visual Basic Editor, then use Insert > Module. Code that lives in the module window is a standard macro you can run by name. Do not paste it into a Sheet object unless you want it to fire on sheet events.
  2. Write the simplest offset. In the module, type:
    Sub WriteRight()
    Selection.Offset(0, 1).Value = "Done"
    End Sub
    The first number is rows, the second is columns. Positive rows go down, negative rows go up; positive columns go right, negative columns go left. Offset(0, 1) therefore means "same row, one column to the right".
  3. Test it deliberately. Close the editor, click a single cell on the sample sheet, then run the macro with Alt+F8, selecting WriteRight and pressing Run. The cell to the right should now read "Done". Repeat with a different starting cell to see that the target always moves with your click.
  4. Read a value instead of writing one. Offset works both ways. MsgBox Selection.Offset(-1, 0).Value displays the cell directly above the selection. Change -1 to 1 and you read the cell below. This is handy for looking up a heading or the previous row's figure without hard-coding addresses.
  5. Move by a whole block. Offset accepts a range too, and it shifts relative to the top-left corner while keeping the same shape. If A2:A10 is selected, Selection.Offset(0, 3).Value = 100 fills D2:D10, not just D2. That is the most useful behaviour and the one most people miss.
  6. Store the result in a variable first when you need the reference. Dim target As Range: Set target = Selection.Offset(1, 0) lets you test, format or copy that cell afterwards. Note the Set keyword - forgetting it is a common cause of run-time error 91.
  7. Guard against running off the sheet. If the selection sits in row 1, Offset(-1, 0) fails because there is no row above. Check boundaries before moving: compare Selection.Row with 1, or wrap the call in error handling with On Error Resume Next and check Err.Number afterwards. A short message such as "Select a cell below the first row" is friendlier than Excel's error dialog.
  8. Work with several selected cells on purpose. Selection can be many cells at once, so a bare MsgBox Selection.Value only reports the first cell. If the user might select a block, either loop through Selection.Cells or insist on a single cell and test with If Selection.Count > 1 Then before continuing.
  9. Save as macro-enabled and reopen. Choose File > Save As and pick Excel Macro-Enabled Workbook (.xlsm). Close and reopen the file, then run the macro again to confirm the code persisted and that the offset still lands where you expect.

Common Mistakes

Swapping rows and columns. The signature is Offset(RowOffset, ColumnOffset). Offset(1, 0) is one row down; Offset(0, 1) is one column right. Mixing them up is the single most frequent error.

Forgetting that Offset returns a range, not a value. Selection.Offset(0, 1) gives you a Range object. You need .Value, .Formula or .Interior.Color to do anything with it, and Set if you assign it to an object variable.

Running the macro with nothing sensibly selected. If a chart, a shape or a whole sheet is selected, Selection is not a Range and the macro stops with a type mismatch. Test whether TypeName(Selection) = "Range" first.

Using Offset where absolute addresses are safer. Offset depends entirely on where the user clicks, so a macro that works in your test can write into the wrong column for someone else. For fixed reports, refer to Range("D2") or use a named range instead.

Trusting ActiveCell and Selection interchangeably. They match when one cell is selected but diverge when a block is selected, where ActiveCell is the one cell inside the block that was clicked first.

Leaving the workbook as .xlsx. Excel warns you on save that macro code will be removed; choosing Yes discards everything you just wrote.

Tips

Use negative offsets to look backwards - reading the cell above is a quick way to grab a column heading. Combine Offset with End(xlDown) or End(xlToRight) to step to the edge of a data block instead of a fixed distance. Apply formatting in one line, for example Selection.Offset(0, 1).Font.Bold = True. When you debug, use F8 to step through line by line and hover over the variables to check the actual cell being addressed. If you plan to share the file, add an instruction cell on the sheet telling users what to select before running the macro - it prevents most support questions.

Troubleshooting

Run-time error 1004: Application-defined or object-defined error. The offset has taken you off the used area of the sheet or into an invalid cell. Check the row or column arithmetic and make sure the starting selection is not adjacent to the boundary.

Nothing appears to happen. The macro may have written somewhere you did not look, or the target cell is hidden, filtered or formatted white on white. Close the editor, click a fresh cell and run again; also confirm the cell is not protected, since a protected sheet blocks writes even when the code is correct.

Error 91: Object variable or With block variable not set. You assigned a range without Set, or the selection was not a range at all. Add Set and validate the selection type.

Type mismatch. Usually a non-range selection - a chart, shape or picture. Nothing is wrong with your Offset logic; guard the code with a type check.

The macro is missing on reopening. The file was saved as .xlsx or the code was placed in a sheet module and the sheet was deleted. Save as .xlsm and keep standard procedures in a normal module.

Frequently Asked Questions

Does Offset change the selection itself?

No. Offset returns a reference to another cell or range; the highlighted area on screen stays where it was unless you add .Select - and selecting inside a macro is usually unnecessary and slower.

What is the difference between Offset and Cells?

Cells(row, column) uses absolute sheet coordinates, while Offset moves relative to a starting point. Use Cells for fixed positions and Offset for "the cell next to whatever is here".

Can I use Offset without a selection?

Yes. Any range can be the starting point: Range("B2").Offset(2, 3) refers to E4. This is generally better practice than relying on the user's click.

Does Offset work on protected sheets?

Offset itself resolves fine, but writing to a locked cell fails. You would need to unprotect the sheet in code with the correct password, which is not something to do silently in a shared file.

Why does Offset(0, 1) land on the wrong cell when several cells are selected?

Offset is applied to the whole selected block from its top-left corner, preserving its size and shape. If you need a single cell, start from ActiveCell instead.

Can I use Offset on a table column?

Yes, once you have a range inside the table. Offsets from a table cell can point outside the table body, so check that the result is still within the rows you intend.