• Skip to main content
  • Skip to primary sidebar
  • Skip to footer

Microsoft Excel Tips and Tricks - Computergaga

  • Home
  • Excel Online Courses
  • Excel Functions List
  • Free Downloads
  • Blog
  • Tutorials
  • About
Home / VBA / Use the Offset Property in VBA

Use the Offset Property in VBA

Excel VBA offers many different ways to refer to ranges in a worksheet. One very popular way is to use the Offset property of the Range object.

The Offset property allows you to move around a worksheet with ease using ranges relative to your current position, or to a specific range.

The Offset property takes two arguments. These relate to the upper left-hand corner of the specified Range object. The first argument is the number of rows to offset, and the second is the number of columns.

The arguments can be positive to move down and to the right, or negative to move up and to the left. For example, the following selects cell C3.

Range(“C2”).Offset(1,0).Select

The Offset property is probably more commonly used with the ActiveCell to move relative to the currently selected cell. The following example is a little more complex. It selects the cell one row above and two columns to the right of the current cell.

ActiveCell.Offset(-1,2).Select

If the currently selected cell was C2, then cell E1 would be selected. If cell C1 was the selected cell then an error message would be returned as it cannot find a Range object that does not exist (there are no rows above row 1).

The example below shows the Offset property being used to select a range of cells 4 rows high and 4 columns wide from the current cell.

Range(ActiveCell,ActiveCell.Offset(4,4)).Select

The Offset property is extremely useful when used in loops and when working with variables. The example below demonstrates a Do While loop being used to enter the numbers 1 to 20 in a column starting from the active cell.

Dim x As Integer
x = 1
Do While x <= 20
   ActiveCell.Value = x
   ActiveCell.Offset(1, 0).Select
   x = x + 1
Loop

As you can see the Offset property can be used in a variety of scenarios. It is the most popular method of moving relatively around a worksheet. In fact if you ever record a macro in Excel with the relative references command turned on, the Offset property is used.

This post serves as an introduction to its syntax and use. Go forth and experiment with some macros.

Reader Interactions

Comments

  1. Misbah says

    28 May 2013 at 4:13 pm

    Hi, i need help making a excel template for a badminton league table.
    can you help

    Reply
    • computergaga_blog says

      30 May 2013 at 8:21 pm

      Have you looked at the Football league table post?
      How is a badminton league table different? How are matches scored?

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Recent Posts

  • Excel SWITCH Function – Nested IF Function Alternative
  • Sum Formulas Only in Excel
  • How to Calculate Age in Excel
  • Custom Sort in Excel
  • Case Sensitive Lookup with XLOOKUP in Excel
Advanced Excel Success book

Footer

Contact

admin@computergaga.com

LinkedIn Twitter YouTube

Useful Links

  • Home
  • Excel Online Courses
  • Tutorials
  • Sitemap
  • Privacy Policy
  • Jobs by Jooble

Course Topics

  • Blog
  • Charts
  • Courses
  • Dashboards
  • Downloads
  • Excel Tips
  • Formulas
  • Mail Merge
  • Office
  • PivotTables
  • Power BI
  • Power Pivot
  • Power Query
  • VBA

Join Our Mailing List

© Copyright 2009 Computergaga · All Rights Reserved ·