You are on page 1of 12

Excel VBA Constructs to Insert Rows

Insert Rows with the Range.Insert Method


Purpose of Range.Insert
Use the Range.Insert method to insert a cell range into a worksheet. The 2 main characteristics of
the Range.Insert method are the following:

1. Range.Insert can insert a single cell or a cell range. For purposes of this VBA Tutorial, you're
interested in inserting entire rows.

2. To make space for the newly-inserted cells, Range.Insert shifts other cells away.

Syntax of Range.Insert
expression.Insert(Shift, CopyOrigin)
“expression” is a Range object. Therefore, I simplify as follows:

Range.Insert(Shift, CopyOrigin)

Parameters of Range.Insert
1. Parameter: Shift.

o Description: Specifies the direction in which cells are shifted away to make space for the
newly-inserted row.

o Optional/Required: Optional.

o Data type: Variant.

o Values: Use a constant from the xlInsertShiftDirection Enumeration:

 xlShiftDown or -4121: Shifts cells down.

 xlShiftToRight or -4161: Shifts cells to the right.

 Default: Excel decides based on the range's shape.


 Usage notes: When you insert a row: (i) use xlShiftDown or -4121, or (ii) omit
parameter and rely on the default behavior.

 Parameter: CopyOrigin.

 Description: Specifies from where (the origin) is the format for the cells in the
newly inserted row copied.

 Optional/Required: Optional.

 Data type: Variant.

 Values: A constant from the xlInsertFormatOrigin Enumeration:

 xlFormatFromLeftOrAbove or 0: Newly-inserted cells take formatting from cells


above or to the left.

 xlFormatFromRightOrBelow or 1: Newly-inserted cells take formatting from cells


below or to the right.

2. Default: xlFormatFromLeftOrAbove or 0.

Newly-inserted cells take the formatting from cells above or to the left.

How to Use Range.Insert to Insert Rows


Use the Range.Insert method to insert a row into a worksheet. Use a statement with the following
structure:

Range.Insert Shift:=xlShiftDown CopyOrigin:=xlInsertFormatOriginConstant


For these purposes:

 Range: Range object representing an entire row.

Use the Worksheet.Rows or Range.EntireRow properties to return a Range object that represents
the entire row. Please refer to the sections about the Rows and EntireRow properties below.

 xlInsertFormatOriginConstant: xlFormatFromLeftOrAbove or xlFormatFromRightOrBelow.

xlFormatFromLeftOrAbove is the default value. Therefore, when inserting rows with formatting
from row above, you can usually omit the CopyOrigin parameter.
You can usually omit the Shift parameter. By default, VBA decides how to shift the cells based on the
range's shape. When inserting a row, this usually results in Excel shifting the cells down.

Specify Rows with the Worksheet.Rows Property


Purpose of Worksheet.Rows
Use the Worksheet.Rows property to return a Range object representing all the rows within the
worksheet the property works with.

Worksheet.Rows is read-only.

Syntax of Worksheet.Rows
expression.Rows
“expression” is a Worksheet object. Therefore, I simplify as follows:

Worksheet.Rows

How to Use Worksheet.Rows to Insert Rows


Use the Worksheet.Rows property to specify the row or rows above which new rows are inserted.

To insert a row, use a statement with the following structure:

Worksheets.Rows(row#).Insert
“row#” is the number of the row above which the row is inserted.

To insert multiple rows, use a statement with the following structure:

Worksheet.Rows(“firstRow#:lastRow#”).Insert
“firstRow#” is the row above which the rows are inserted. The number of rows VBA inserts is calculated
as follows:

lastRow# – firstRow# + 1

Specify the Active Cell with the Application.ActiveCell Property


Purpose of Application.ActiveCell
Use the Application.ActiveCell property to return a Range object representing the active cell.

Application.ActiveCell is read-only.

Syntax of Application.ActiveCell
expression.ActiveCell
“expression” is the Application object. Therefore, I simplify as follows:

Application.ActiveCell

How to Use Application.ActiveCell To Insert Rows


When you insert a row, use the Application.ActiveCell property to return the active cell. This allows you
to use the active cell as reference for the row insertion operation.

Use the Range.Offset property to return a Range object a specific number of rows above or below the
active cell. Use the Range.EntireRow property to return a Range object representing the entire row or
rows above which to insert the new row. Please refer to the sections about the Offset and EntireRow
properties below.

To insert a row above the active cell, use the following statement:

ActiveCell.EntireRow.Insert Shift:=xlShiftDown
To insert a row a specific number of rows above or below the active cell, use a statement with the
following structure:

ActiveCell.Offset(RowOffset).EntireRow.Insert Shift:=xlShiftDown

Specify a Cell Range with the Worksheet.Range Property


Purpose of Worksheet.Range
Use the Worksheet.Range property to return a Range object representing a single cell or a cell
range.

Syntax of Worksheet.Range
expression.Range(Cell1, Cell2)
“expression” is a Worksheet object. Therefore, I simplify as follows:

Worksheet.Range(Cell1, Cell2)

Parameters of Worksheet.Range
1. Parameter: Cell1.

o Description:

 If you use Cell1 alone (omit Cell2), Cell1 specifies the cell range.
 If you use Cell1 and Cell2, Cell1 specifies the cell in the upper-left corner of the cell
range.

 Required/Optional: Required.

 Data type: Variant.

 Values:

 If you use Cell1 alone (omit Cell2): (i) range address as an A1-style reference in


language of macro, or (ii) range name.

 If you use Cell1 and Cell2: (i) Range object, (ii) range address, or (iii) range name.

 Parameter: Cell2.

 Description: Cell in the lower-right corner of the cell range.

 Required/Optional: Optional.

 Data type: Variant.

 Values: (i) Range object, (ii) range address, or (iii) range name.

How to Use Worksheet.Range to Insert Rows


When you insert a row, use the Worksheet.Range property to return a cell or cell range. This allows you
to use a specific cell or cell range as reference for the row insertion operation.

Use the Range.Offset property to return a Range object a specific number of rows above or below the
cell or cell range. Use the Range.EntireRow property to return a Range object representing the entire
row or rows above which to insert the new row or rows. Please refer to the sections about the Offset and
EntireRow properties below.

To insert rows above the cell range specified by Worksheet.Range, use a statement with the
following structure:

Worksheet.Range(Cell1, Cell2).EntireRow.Insert Shift:=xlShiftDown


To insert rows a specific number of rows above or below the cell range specified by
Worksheet.Range use a statement with the following structure:
Worksheet.Range(Cell1, Cell2).Offset(RowOffset).EntireRow.Insert Shift:=xlShiftDown
If the cell range represented by the Worksheet.Range property spans more than 1 row, the Insert
method inserts several rows. The number of rows inserted is calculated as follows:

lastRow# – firstRow# + 1
Please refer to the section about the Worksheet.Rows property above for further information about this
calculation.

Specify a Cell with the Worksheet.Cells and Range.Item Properties


Purpose of Worksheet.Cells and Range.Item
Use the Worksheet.Cells property to return a Range object representing all the cells within a
worksheet.

Once your macro has all the cells within the worksheet, use the Range.Item property to return a Range
object representing one of those cells.

Syntax of Worksheet.Cells and Range.Item


Worksheet.Cells
expression.Cells
“expression” is a Worksheet object. Therefore, I simplify as follows:

Worksheet.Cells
Range.Item
expression.Item(RowIndex, ColumnIndex)
“expression” is a Range object. Therefore, I simplify as follows:

Range.Item(RowIndex, ColumnIndex)
Worksheet.Cells and Range.Item Together
Considering the above:

Worksheet.Cells.Item(RowIndex, ColumnIndex)
However, Item is the default property of the Range object. Therefore, you can generally omit the Item
keyword before specifying the RowIndex and ColumnIndex arguments. I simplify as follows:

Worksheet.Cells(RowIndex, ColumnIndex)

Parameters of Worksheet.Cells and Range.Item


1. Parameter: RowIndex.

o Description:

 If you use RowIndex alone (omit ColumnIndex), RowIndex specifies the index of the
cell you work with. Cells are numbered from left-to-right and top-to-bottom.

 If you use RowIndex and ColumnIndex, RowIndex specifies the row number of the
cell you work with.

 Required/Optional: Required.

 Data type: Variant.

 Values: You usually specify RowIndex as a value.

 Parameter: ColumnIndex.

 Description: Column number or letter of the cell you work with.

 Required/Optional: Optional.

 Data type: Variant.

 Values: You usually specify ColumnIndex as a value (column number) or letter


within quotations (“”).

How to use Worksheet.Cells and Range.Item to Insert Rows


When you insert a row, use the Worksheet.Cells and Range.Item properties to return a cell. This allows
you to use a specific cell as reference for the row insertion operation.

Use the Range.Offset property to return a Range object a specific number of rows above or below the
cell. Use the Range.EntireRow property to return a Range object representing the entire row above
which to insert the row. Please refer to the sections about the Offset and EntireRow properties below.

To insert a row above the cell specified by Worksheet.Cells, use a statement with the following
structure:
Worksheet.Cells(RowIndex, ColumnIndex).EntireRow.Insert Shift:=xlShiftDown
To insert a row a specific number of rows above or below the cell specified by Worksheet.Cells,
use a statement with the following structure:

Worksheet.Cells(RowIndex, ColumnIndex).Offset(RowOffset).EntireRow.Insert Shift:=xlShiftDown

Specify a Cell Range a Specific Number of Rows Below or Above a


Cell or Cell Range with the Range.Offset Property
Purpose of Range.Offset
Use the Range.Offset property to return a Range object representing a cell range located a number
of rows or columns away from the range the property works with.

Syntax of Range.Offset
expression.Offset(RowOffset, ColumnOffset)
“expression” is a Range object. Therefore, I simplify as follows:

Range.Offset(RowOffset, ColumnOffset)

Parameters of Range.Offset
1. Parameter: RowOffset.

o Description: Number of rows by which cell or cell range is offset.

o Required/Optional: Optional.

o Data type: Variant.

o Values:

 Positive number: Moves down the worksheet.

 Negative number: Moves up the worksheet.

 0: Stays on the same row.


 Default: 0.

Stays on the same row.

 Parameter: ColumnOffset.

 Description: Number of columns by which cell or cell range is offset.

 Required/Optional: Optional.

 Data type: Variant.

 Values:

 Positive number: Moves towards the right of the worksheet.

 Negative number: Moves towards the left of the worksheet.

 0: Stays on the same column.

 Default: 0.

Stays on the same column.

 Usage notes: When you insert a row, you can usually omit the ColumnOffset
parameter. You're generally interested in moving a number of rows (not columns) above or
below.

How to Use Range.Offset to Insert Rows


When you insert a row, use the Range.Offset property to specify a cell or cell range located a
specific number of rows below above another cell or cell range. This allows you to use this new cell
or cell range as reference for the row insertion operation.

Use properties such as Application.ActiveCell, Worksheet.Range and Worksheet.Cells to specify the


base range the Offset property works with. Please refer to the sections about the ActiveCell, Range and
Cells properties above.

Specify Entire Row with the Range.EntireRow Property


Purpose of Range.EntireRow
Use the Range.EntireRow property to return a Range object representing the entire row or rows
containing the cell range the property works with.

Range.EntireRow is read-only.

Syntax of Range.EntireRow
expression.EntireRow
“expression” is a Range object. Therefore, I simplify as follows:

Range.EntireRow

How to Use Range.EntireRow to Insert Rows


When you insert a row, use the Range.EntireRow property to return the entire row or rows above
which the new row or rows are inserted.

Use properties such as Application.ActiveCell, Worksheet.Range and Worksheet.Cells to specify the


range the EntireRow property works with. Please refer to the sections about the ActiveCell, Range and
Cells properties above.

Clear Row Formatting with the Range.ClearFormats Method


Purpose of Range.ClearFormats
Use the Range.ClearFormats method to clear the formatting of a cell range.

Syntax of Range.ClearFormats
expression.ClearFormats
“expression” is a Range object. Therefore, I simplify as follows:

Range.ClearFormats

How to Use Range.ClearFormats to Insert Rows


The format of the newly-inserted row is specified by the CopyOrigin parameter of the Range.Insert
method. Please refer to the description of Range.Insert and CopyOrigin above.

When you insert a row, use the Range.ClearFormats method to clear the formatting of the newly-
inserted rows. Use a statement with the following structure after the statement that inserts the new
row (whose formatting you want to clear):

Range.ClearFormats
“Range” is a Range object representing the newly-inserted row.
Use the Worksheet.Rows or Range.EntireRow properties to return a Range object that represents the
newly-inserted row. Please refer to the sections about the Rows and EntireRow properties above.

Copy Rows with the Range.Copy Method


Purpose of Range.Copy
Use the Range.Copy method to copy a cell range to another cell range or the Clipboard.

Syntax of Range.Copy
expression.Copy(Destination)
“expression” is a Range object. Therefore, I simplify as follows:

Range.Copy(Destination)

Parameters of Range.Copy
1. Parameter: Destination.

o Description: Specifies the destination cell range to which the copied cell range is copied.

o Required/Optional: Optional parameter.

o Data type: Variant.

o Values: You usually specify Destination as a Range object.

o Default: Cell range is copied to the Clipboard.

o Usage notes: When you insert a copied row, omit the Destination parameter to copy the
row to the Clipboard

How to Use Range.Copy to Insert Rows


Use the Range.Copy method to copy a row which you later insert.

Use a statement with the following structure before the statement that inserts the row:

Range.Copy
“Range” is a Range object representing an entire row.
Use the Worksheet.Rows or Range.EntireRow properties to return a Range object that represents a
row. Please refer to the sections about the Rows and EntireRow properties above.

You might also like