Deleting Zero Values from a Data Table

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


1

Robert would like a macro that finds all instances of cells in a data table that have a zero value and, then, deletes those cells. He wonders how to go about doing this.

For the purposes of this tip, I'm going to assume that Robert actually wants to clear the values in the cells, not actually delete the cells. Further, I'm going to assume that he doesn't want to just hide zero values, as can be easily done with a simple setting change in Excel. (How you do this has been covered in other ExcelTips.)

There are actually a few ways you can go about getting rid of your zero values. One way doesn't even use macros, but instead relies upon the Find and Replace capabilities of Excel:

  1. Press Ctrl+H. Excel displays the Replace tab of the Find and Replace dialog box.
  2. Click the Options button to expand the dialog box. (See Figure 1.)
  3. Figure 1. The expanded Replace tab of the Find and Replace dialog box.

  4. In the Find What box, enter 0.
  5. Make sure the Replace With box is empty.
  6. Select the Match Entire Cell Contents check box.
  7. Click Replace All to perform the replacements.

You could even, if desired, translate the above steps into a short little macro:

Sub ReplaceZeros()
    Cells.Replace What:="0", Replacement:="", _
      LookAt:=xlWhole, SearchOrder:=xlByRows
End Sub

There is a limit to what this approach will accomplish, most notably it will only replace cells that actually contain a 0 value. It will not replace formulas that resolve to 0. For instance, if a cell contained the fomula =4-4, which resolves to 0, the Find and Replace operation ignores it because there is not actually a 0 value in the cell.

If you want something that will actually clear cells that either contain 0 or resolve to 0, then you'll need a different macro approach. Here's a good one:

Sub DeleteZeroes()
    Dim rCell As Range

    For Each rCell In Selection
        If rCell.Value = 0 Then
            rCell.ClearContents
        End If
    Next
End Sub

To use the macro, simply select the cells you want to affect and then run the macro. It looks through each of the selected cells and removes the contents of any cell that contains a 0 value—and in this case that also includes any formulas that resolve to 0.

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 (13437) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021.

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

Printing a Number of Different Pages

If you don't need to print an entire workbook, it can be confusing to figure out how to print just certain pages. This ...

Discover More

Shortcut Key to Delete a Paragraph

There are numerous ways you can delete paragraphs as you are editing your document. This tip looks at a couple of the ...

Discover More

Opening Multiple Workbooks at Once

Need to open a bunch of workbooks from within Excel? It's easy to do when you construct a selection set in the Open ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More ExcelTips (ribbon)

Removing Pictures for a Worksheet in VBA

Excel allows you to add pictures to your worksheet, even within a macro. However, you might have a bit harder time ...

Discover More

Adding a Calendar to a Worksheet

Using a specialized calendar control is a great way to let users add dates to a worksheet. Unfortuantely, Microsoft ...

Discover More

Finding Workbooks Containing Macros

Workbooks can contain macros, or not. It is entirely up to you whether they do or not, but at some future time you might ...

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 one less than 9?

2022-04-10 10:05:42

J. Woolley

For Excel's option to hide zero values see the following:
https://support.microsoft.com/en-us/Search/results?query=Display+or+hide+zero+values
The option is toggled ON/OFF by the worksheet’s Window property DisplayZeros. For the active worksheet, the following VBA statement will toggle it:
    ActiveWindow.DisplayZeros = Not ActiveWindow.DisplayZeros
To hide zero values for all worksheets in the active workbook:
    Dim WS as Worksheet
    For Each WS In ActiveWorkbook.Worksheets
        WS.Activate
        ActiveWindow.DisplayZeros = False
    Next WS
My Excel Toolbox includes the WindowDressing macro, which will toggle several such properties for the active worksheet or all worksheets. A screenshot is included in my recent comment added to the following Tip:
https://excelribbon.tips.net/T009308_Controlling_Where_You_Edit_Cell_Contents.html


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.