Skip to main content
A newer version of this page is available. .

How to: Delete a Row or Column from a Worksheet

  • 3 minutes to read

Row

This example demonstrates how to delete rows from a worksheet. You can do this in one of the following ways.

  • Call the Row.Delete method of the Row object representing the row to be deleted.
  • Call the RowCollection.Remove method of the object that is accessed via the Worksheet.Rows property. Pass the zero-based index of a row to be deleted as a parameter. To delete multiple rows, pass the number of rows to be deleted as well.
  • To delete a row containing the specified cell or multiple rows containing the specified range of cells, use the Worksheet.DeleteCells method with the cell or cell range and DeleteMode.EntireRow enumeration member passed as parameters.

When you delete rows from a worksheet, other rows are automatically shifted up.

// Delete the 2nd row from the worksheet.
worksheet.Rows[1].Delete();

// Delete the 3rd row from the worksheet.
worksheet.Rows.Remove(2);

// Delete three rows from the worksheet starting from the 10th row.
worksheet.Rows.Remove(9, 3);

// Delete a row that contains the "B2"cell.
worksheet.DeleteCells(worksheet.Cells["B2"], DeleteMode.EntireRow);

You can also hide worksheet rows. See the How to: Hide a Row or a Column topic for details.

Note

The number of rows in a worksheet is permanently fixed - 1048576.

Column

This example demonstrates how to delete columns from a worksheet. You can do this in one of the following ways.

  • Call the Column.Delete method of the Column object representing the column to be deleted.
  • Call the ColumnCollection.Remove method of the object that is accessed via the Worksheet.Columns property. Pass the zero-based index of a column to be deleted as a parameter. To delete multiple columns, pass the number of columns to be deleted as well.
  • To delete a column containing the specified cell or multiple columns containing the specified range of cells, use the Worksheet.DeleteCells method with the cell or cell range and DeleteMode.EntireColumn enumeration member passed as parameters.

When you delete columns from a worksheet, other columns are automatically shifted to the left.

// Delete the 2nd column from the worksheet.
worksheet.Columns[1].Delete();

// Delete the 3rd column from the worksheet.
worksheet.Columns.Remove(2);

// Delete three columns from the worksheet starting from the 10th column.
worksheet.Columns.Remove(9, 3);

// Delete a column that contains the "B2"cell.
worksheet.DeleteCells(worksheet.Cells["B2"], DeleteMode.EntireColumn);

You can also hide worksheet columns. See the How to: Hide a Row or a Column topic for details.

Note

The number of columns in a worksheet is permanently fixed - 16384.

See Also