Hello Excellers and welcome to another Excel #Excel tips #MacroMonday bog post in my 2020 macro series. Today I want to share with you a simple macro. This small bit of code will clear all cells with values that are greater than zero. I used this macro last week to clear a really large data set of all values that were over zero. I only needed the zero values to left as I was looking for gaps in my data. In this case zero sales.
Preparing To Write The Macro
First, you will need to open the Visual Basic Editor. There are two ways to do this. Either by hitting ALT +F11 or selecting the Developer Tab | Code Group | Visual Basic. Both methods have the same result. You then have a choice. You can either create a module to store your code either in your Personal Macro Workbook or in your current workbook. What’s the difference?. If you save the macro in your Personal Macro workbook it will be available for use in any of my Excel workbooks. If you store it in the current workbook then use is restricted to that workbook.
Learn More About Your Personal Macro Workbook (PMW)
If you want to read more about your Excel PMW then check out my blog posts below.
Starting The Macro
We need to start off the macro by inserting a New Module. Do this by selecting the Personal.xlsbworkbook, then Insert Module.
Type Sub then the name of your macro. In this example, I have called the macro LeaveZeros. Notice that Excel will automatically enter the end text End Sub to end the Sub Routine. We simply need to enter the rest of the code between these two lines to clear our cells.
Turn Off Screen Updating
By turning off screen updating we avoid the screen flashing or flickering as the Excel executes the VBA code. Any time a change occurs on your Excel worksheet, the screen will update.
The next step in writing our macro is to declare some variables. This ensures that Excel creates a memory container for these values. In this example, there is only one variable to declare. We use the Range Object to represent the user-selected range and set that range as the selection.
Using The For Each Loop
Next, we instruct Excel to use the For Each Looping command. We start with For Each and instruct Excel to loop through all cells in the myRange variable. If the cell contents are greater than zero then the values will be cleared. This looping continues until all cells in the selected range have been analyzed.
Turn Screen Updating Back On
We now return the screen updating to back on. This resumes normal Excel behaviour for changes on the worksheet.
Ending The Excel Macro
The code ends once all looping of cells has been completed with the “End Sub" piece of code. This was already entered into the module for us when started the type the name of the macro.
More Related #MacroMonday Excel Blog Posts
If you want more tips then sign up to my monthly Newsletter where I share 3 Excel Tips on the first Wednesday of the month and receive my free Ebook, 30 Excel Tips.
Likewise, if you want to see all of the blog posts in the Macro Mondays Series Click The Link Below