Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Testing for an Empty Worksheet.

Testing for an Empty Worksheet

Written by Allen Wyatt (last updated October 9, 2021)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


Michael has a macro that prints a number of worksheets. Occasionally one or two of the worksheets to be printed may contain no data. He is looking for a technique to test whether a worksheet contains data, and then only print those worksheets.

There are several ways you can go about testing for an empty worksheet. Of course, it depends on what you really mean by "empty," at least to a degree. For instance, if a worksheet has absolutely nothing in it—nothing in any cell of the worksheet—we could consider it empty. However, you might have a worksheet that contains some column headings that you added, but nothing except those headings. While Excel would consider the worksheet not empty, you might consider it empty for printing purposes.

Perhaps the easiest way to check if a worksheet is empty is to use the UsedRange object to deterrnine what is in the worksheet:

With ActiveSheet.UsedRange
    IsSheetEmpty = .Rows.Count = 1 And _
      .Columns.Count = 1 And _
      .Cells(1,1) = ""
End With

Note that the UsedRange object consists of, well, the range of used cells within a worksheet. Thus, if the count of rows in this range is 1 and the count of columns in this range is 1, and there is nothing in the first cell of the UsedRange object, then the worksheet is probably empty.

Depending on the characteristics of your data, you may be able to shorten the code even more:

With ActiveSheet.UsedRange
    IsSheetEmpty = .Count = 1 And .Cells(1,1) = ""
End With

In this version you are simply checking to see if the count of cells in the UsedRange object is 1 and if that particular cell is empty.

If you have a header row (or two) in your worksheet, then you can adjust this technique to however may rows and columns you have in those headers. For instance, if you have headers in the range A1:F4, then you might adjust the technique in this manner:

IsSheetEmpty = ActiveSheet.UsedRange.Rows.Count=4 _
  AND ActiveSheet.UsedRange.Columns.Count=6

You don't need to check the contents of A1 in this instance because you already know that it (and several other cells) contain information—your headers. You just want to ignore everything in those headers to determine if there is additional information in the worksheet.

If the worksheet is completely empty (no header information that you've added), you can use the CountA worksheet function to analyze the cells in the worksheet. If the result of the function is greater than zero, then the worksheet is not empty. For example, let's say that the worksheet you want to analyze is specified by the object sht. You can use this technique in this manner:

IsSheetEmpty = Application.WorksheetFunction.CountA(sht.Cells) = 0

Of course, it is possible for a worksheet to contain items other than information in cells. If you suspect you will have these types of objects in a worksheet (things like AutoShapes, graphics, or embedded charts), then your testing for "emptiness" will need to be more complete. Each of these items are contained within collections that are accessible in VBA, and you can check the Count property for each collection to see if it is zero or not.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (9637) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel here: Testing for an Empty Worksheet.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Displaying Edits by Date

Track Changes is a great tool; it allows you to see what changes were made in a document and then determine whether you ...

Discover More

Empty Cells Triggers Error

By default, Excel provides some feedback on your formulas so that you can easily locate potential errors. If you get ...

Discover More

Using Drag-and-Drop to Create a Hyperlink

If you open workbooks in two instances of Excel, you can use drag-and-drop techniques to create hyperlinks from one ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More ExcelTips (ribbon)

Detecting Types of Sheets in VBA

When processing workbook information in a macro, you may need to step through each worksheet to make some sort of ...

Discover More

Workaround for Multiple Rows of Worksheet Tabs

If you've got a lot of worksheets in your workbook, you may want to display their tabs in to rows at the bottom of the ...

Discover More

Dynamically Changing Worksheet Tab Color

Excel makes it easy to change the color of a worksheet's tab. If you want that color change to be dynamic, one way to do ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is nine minus 5?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.