«

»

Jan
21
2012

Adding/Removing Excel Rows/Columns from VBA

You can manipulate (add/remove) rows or columns in excel through Excel VBA code. This could be useful for variety of purposes or while developing applications.

Here are some examples of how to do it:

 

The following code inserts the a row at the location of row 9.

‘bring in focus into row 9
Rows(“9:9″).Select

‘perform insertion
Selection.Insert Shift:=xlDown

 

Similarly if we want rows to be inserted or deleted then the selection range to be expanded accordingly.

For example the following code will delete the rows 9 to 13

‘make the selection
Rows(“9:13″).Select

‘perform deletion
Selection.Delete Shift:=xlUp

 

For manipulating the columns the syntax is very similar,

The following code will insert a new excel column at the location of Column B

‘make the selection
Columns(“B:B”).Select

‘perform insertion
Selection.Insert Shift:=xlToRight

 

The following code will delete 3 excel columns (B to D)

‘make the selection
Columns(“B:D”).Select

‘perform deletion
Selection.Delete Shift:=xlToLeft

 

Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>