Detecting Empty Worksheets

Check for Populated Cells

Worksheets can have one or more cells populated with values where a value can be simple (text, numeric, date/time) or a formula or a formula‑based value. In such a case, it is easy to detect if a given worksheet is empty or not. All we have to check is the Cells.MaxDataRow or Cells.MaxDataColumn properties. If the aforementioned properties return zero or positive values, that means one or more cells have been populated; however, if any of these properties return -1, that indicates that no cells have been populated in the given worksheet.

Check for Empty Initialized Cells

All cells which have values are automatically initialized, however, there is a possibility that a worksheet has cells with only formatting applied. In such a scenario, the Cells.MaxDataRow or Cells.MaxDataColumn properties will return -1, indicating the absence of any populated values. However, initialized cells that contain only formatting cannot be detected using this approach. In order to check if a worksheet has initialized cells (even if they are empty), it is advised to use the IEnumerator.MoveNext method on the enumerator acquired from the Cells collection. If the IEnumerator.MoveNext method returns true, that means there is at least one initialized cell in the given worksheet.

Check for Shapes

It is possible that a given worksheet does not have any populated cells; however, it could contain shapes and objects such as controls, charts, images, and so on. If we need to check whether a worksheet contains any shapes, we can inspect the ShapeCollection.Count property. Any positive value indicates the presence of shape(s) in the worksheet.

Programming Sample