Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Testing for an Empty Worksheet.

Testing for an Empty Worksheet

Written by Allen Wyatt (last updated March 19, 2022)
This tip applies to Excel 97, 2000, 2002, and 2003


1

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:

IsSheetEmpty = ActiveSheet.UsedRange.Rows.Count=1 _
  AND ActiveSheet.UsedRange.Columns.Count=1 _
  AND Cells(1,1).Value=""

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 cell A1, then the worksheet is probably 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 (3280) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) 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

Disabling Dragging and Dropping

Excel allows you to easily paste information into a worksheet, including through simply dragging and dropping the ...

Discover More

Cannot Set Heading Rows in a Table

Word allows you to specify which rows in a table should be considered headings. What if setting the headings doesn't work ...

Discover More

Cleaning Up Text in a Macro

Need to remove extraneous characters from a text string? VBA makes it easy through the CleanString method, described in ...

Discover More

Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!

More ExcelTips (menu)

Reversing Cell Contents

Macros are great at working with text. This tip presents an example that shows this versatility by reversing the contents ...

Discover More

Running Macros in the Background

Want to run a macro in Excel, but not sure if doing so will tie up your computer? Here's how macro processing really happens.

Discover More

Saving a Workbook in a Macro

Does your macro need to make sure that the workbook being processed is saved to disk? You can add the saving capability ...

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 seven more than 1?

2023-02-27 16:18:38

Giovanni Vannucci

To check if a worksheet is empty, I have been using the following:

IsSheetEmpty = IsEmpty( ActiveSheet.UsedRange )

I believe that it works because:
a) if UsedRange is more than one cell, it gets converted to a variant containing an array; and
b) if UsedRange is a single cell, it gets converted to a variant containing the value of that cell.
Therefore, it is true if and only if UsedRange is a single cell that has not been initialized (has a value of Empty).

Is this a reasonable alternative to what you suggest?


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon 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.