# Deleting Zero Values from a Data Table

by Allen Wyatt
(last updated April 9, 2016)

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, and 2016.

##### 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

Got a document that has a whole raft of e-mail address in it? You can easily convert all of them to clickable hyperlinks ...

Discover More

Counting Alphabetic Characters in a String

Need to find out how many times a certain letter appears in a text string? It's easy to do if you rely on the SUBSTITUTE ...

Discover More

Dealing with Long Formulas

If your worksheet formulas seem to go on forever, here's a handy way to make them more understandable. (All you need to ...

Discover More

Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!

##### More ExcelTips (ribbon)

Determining if Calculation is Necessary

When processing a worksheet with a macro, it may be helpful to periodically recalculate the worksheet. Wouldn't it be ...

Discover More

Determining If a Number is Odd or Even

If you need to know whether a particular value is odd or even, you can use this simple formula. Designed to be used in a ...

Discover More

Got a workbook cluttered with all sorts of macros? Delete them and you'll make your workbook easier to manage.

Discover More
##### Subscribe

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

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}] 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 4 + 8?

2019-06-24 12:52:22

TEAC

This is my first time writing a macro from almost scratch but i think i made a better version for my needs at least.

So to remove all the zeros (value of 0 or equation equals 0) in a selection you define before execution:

Sub removezeros()
Dim aoi As Range
Set aoi = Selection
For Each cell In aoi
If cell.Value = 0 Then
cell.ClearContents
End If
Next
MsgBox "FINISHED", vbCritical
End Sub

aoi stands for area of interest (to keep it different to 'Selection' which needs to be used). Message box displayed at the end to show it has run.

2019-06-12 06:26:47

Willy Vanhaelen

@Kristian
You are wrong. See point 5. of the above instructions to use Find and Replace as wel as the xlWhole in the first macro.

2019-06-11 20:48:19

Kristian

You must be careful using the find/replace function, or these macros for that matter, as these will remove ALL occurrences of the number zero, which includes if it occurs in 10, 300 etc which it will now make 1 and 3 respectively,

2016-04-11 12:42:30

Michael (Micky) Avidan

@Rod Grealish,
(see Figure 1 below)
--------------------------
Michael (Micky) Avidan
“Microsoft® Answers" - Wiki author & Forums Moderator
“Microsoft®” MVP – Excel (2009-2016)
ISRAEL

Figure 1.

2016-04-11 06:54:46

Rod Grealish

As I understand the question and answer, Robert, the originator of the question, wants to remove zero entries not just hide them. The tip answers that question.

2016-04-11 04:14:20

Paul B

You could also use conditional formatting where you set the value to equal to zero and text colour to white.

This helps if you have formulas in the cells that produce a zero value

2016-04-09 10:32:09

John Downes

Why not just suppress any unwanted zeros using format?

Selection.NumberFormat = "#,##0;-#,##0;;@"

2016-04-09 04:55:40

John Gooch

Why not click 'File', 'Options', Advanced', and then unclick the box "Show a zero in cells that have zero value'?.

This applies to the whole sheet

This always works for me . . . .

##### 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.