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 then that means, one or more cells have been populated, however, if any of these properties return -1, that indicates that none of the 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 but initialized cells due to the cell formatting cannot be detected using this approach. In order to check if a worksheet has empty initialized cells, it is advised to use the Iterator.hasNext method on iterator acquired from Cells collection. If the iterator.hasNext method returns true then that means there are one or more initialized cells 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 & objects such as controls, charts, images and so on. If we need to check if a worksheet contains any shape, we can do it by inspecting the ShapeCollection.Count property. Any positive value indicates the presence of shape(s) in the worksheet.

Programming Sample