Flag This Hub

What is Offset in VBA(Visual Basic)?

By


Offset(0,1)

Offset property in Visual Basic lets you move around the cells in your worksheet in reference to your current position.

The offset code is like this, Range(“Cell Position”).Offset(Row Position, Column Position).

Let's look fig. 1 as an example.


Fig. 1
Fig. 1

When and how to use offset?

The conversion of this image to VBA code should be "Range(“C2”).Offset(0,-2).Select". This means that the yellow cell is C2 and as well as the integer 0 while -2 is the orange column.

  • If you want to move one cell down from the yellow cell, C2, then the code should be Range("C2").Offset(1, 0).Select.
  • If you want to move one cell up from the yellow cell, C2, then the code should be Range("C2").Offset(-1, 0).Select.
  • If you want to move one cell to the right from the yellow cell, C2, then the code should be Range("C2").Offset(0, 1).Select.
  • If you want to move one cell to the left from the yellow cell, C2, then the code should be Range("C2").Offset(0, -1).Select.

Offset could be a great help of you want to take effect something on a particular that depends on the value of other cell. I hope this little explanation could help you understand and use the offset code really well in your Visual Basic Macro programming.



Intelligence Test

Comments

Chris 3 months ago

Thanks for this post. I have understood it well.

jasper 3 months ago

Thanks for this blog..very useful..

Aiza 3 months ago

Thanks for this blog..Very detailed in your explanation..

Sania 13 days ago

good info

Submit a Comment
Members and Guests

Sign in or sign up and post using a hubpages account.



    Like this Hub?
    Please wait working