Professional Documents
Culture Documents
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.
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.
2. Default: xlFormatFromLeftOrAbove or 0.
Newly-inserted cells take the formatting from cells above or to the left.
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.
Worksheet.Rows is read-only.
Syntax of Worksheet.Rows
expression.Rows
“expression” is a Worksheet object. Therefore, I simplify as follows:
Worksheet.Rows
Worksheets.Rows(row#).Insert
“row#” is the number of the row above which the row is inserted.
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
Application.ActiveCell is read-only.
Syntax of Application.ActiveCell
expression.ActiveCell
“expression” is the Application object. Therefore, I simplify as follows:
Application.ActiveCell
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
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:
Parameter: Cell2.
Required/Optional: Optional.
Data type: Variant.
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:
lastRow# – firstRow# + 1
Please refer to the section about the Worksheet.Rows property above for further information about this
calculation.
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.
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)
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.
Parameter: ColumnIndex.
Required/Optional: Optional.
Data type: Variant.
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:
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 Required/Optional: Optional.
o Data type: Variant.
o Values:
Parameter: ColumnOffset.
Required/Optional: Optional.
Data type: Variant.
Values:
Default: 0.
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.
Range.EntireRow is read-only.
Syntax of Range.EntireRow
expression.EntireRow
“expression” is a Range object. Therefore, I simplify as follows:
Range.EntireRow
Syntax of Range.ClearFormats
expression.ClearFormats
“expression” is a Range object. Therefore, I simplify as follows:
Range.ClearFormats
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.
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 Usage notes: When you insert a copied row, omit the Destination parameter to copy the
row to the Clipboard
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.