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:
Figure 1. The expanded Replace tab of the Find and Replace dialog box.
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.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (13437) applies to Microsoft Excel 2007, 2010, 2013, and 2016.
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!
Want to add some macros to your workbook? What do you do if you try to add the macros but the program has disabled the tools?Discover More
The Text-to-Columns tool is an extremely powerful feature that allows you to divide data in a variety of ways. Excel even ...Discover More
Need to know the character code used for a particular character? In a macro you can use the Asc function to determine the ...Discover More
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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.