20180115

VBA Macro to move cell pointer in EXCEL




Start with simple macro VBA script writing to move cell pointer around the sheet. With this you can reference your cell value in your codes. Simple VBA keyword will help you to move the cell pointer. Let us see the example to move around the cell pointer


 Using ACTIVECELL, OFFSET keywords in VBA you will be able to move the cell pointer.

ACTIVECELL keyword determines the cell where you cell pointer is situated
OFFSET keyword with parameter determines the desired row number and column number












In this animated image example our activecell is "D5" and we want to write macro to move 1 row and 1 column up-down around cell "D5"


Explained steps:
 Let me let you know the syntax or properties of OFFSET keyword in VBA
            OFFSET([RowOffset],[ColumnOffset]) as range where we need to assign the desired row number and column number.
            If you want to move cell pointer upward from active cell by 2 rows and in same column then you should write:    
                    Activecell.Offset(-2,0).Select
             This piece of code will take your cell pointer 2 rows upward in same columns.
                    Numbering the rows and columns in Offset is mentioning cell properties.

Please see below VBA code and try.

         'Firstly set cell pointer to Cell D5
             Range("D5").Select

         'Moving cell pointer 1 row above in same column
             ActiveCell.Offset(-1, 0).Select


As I said Activecell is nothing the cell where cell pointer is currently situated. Offset with




         'Moving cell pointer back to cell D5             Range("D5").Select
         'Move cell pointer 1 row above and 1 column to left             ActiveCell.Offset(-1, -1).Select
         'Moving cell pointer back to cell D5             Range("D5").Select
         'Move cell pointer in same row and 1 column to left             ActiveCell.Offset(, -1).Select
         'Moving cell pointer back to cell D5             Range("D5").Select
         'Move cell pointer to 1 row below and 1 column to left             ActiveCell.Offset(1, -1).Select
         'Moving cell pointer back to cell D5             Range("D5").Select
         'Move cell pointer in 1 row below on same column             ActiveCell.Offset(1, 0).Select
         'Moving cell pointer back to cell D5             Range("D5").Select
         'Move Cell pointer to 1 row below and 1 column to right             ActiveCell.Offset(1, 1).Select
         'Moving cell pointer back to cell D5             Range("D5").Select
         'Move cell pointer in same row and 1 column to right             ActiveCell.Offset(, 1).Select
         'Moving cell pointer back to cell D5             Range("D5").Select
         'Move 1 row above and 1 column to right             ActiveCell.Offset(-1, 1).Select

In next article we will see how to optimize the above code to run faster.




2 comments:

Search This Blog

Reverse String using Excel VBA

Normally this is an interview question being asked by company technical recruiters. This is an logical question and every aspirants must b...