Excel How-to: A Macro to Hide Blank Rows on a Spreadsheet


Today, a coworker asked me how to hide a row on his Excel spreadsheet.  This was a fairly large spreadsheet, with about 150 rows and 30 columns.  After showing him, he started to hide every other row, which would be an incredible waste of time for someone in his salary bracket.  So I offered him a solution: a macro that hides (and unhides) the rows by pushing a button.  Really two buttons.  Here’s how I did it:

We’ll start with a simple spreadsheet.  You can follow along with this one: Hide_Row_Excel_File_No_Macros.xls

If you’d rather view a completed file with the macros already present, click this one: Hide_Row_Excel_File.xls

Here’s what the raw spreadsheet looks like:

hr011

We want to be able to hide and display the blank rows, so we’ll need to write a couple of macros.  First, we’ll put a formula in column D that checks if column C is blank.  The formula =if(C6=”",”HIDE”,”") is entered in cell D6:

hr021

Then, we’ll copy the formula from cell D6 down the column through cell D40:

hr031

The macro will run on a simple loop, repeating for the necessary number of times.  We’ll assign a number to each row to determine that number.  It just a formula that adds one to the cell’s value above it in the column.  We’ll enter the formula +E5+1 in cell E6:

hr041

We’ll copy this formula from cell E6 down through cell E40:

hr051

Next, we’ll establish the number of times the macro needs to run.  We’ll use the formula =max(E6:E40) to determine the number of repetitions, and enter it in cell G1. We’ll enter a zero in cell F1; this will be used as a counter:

hr062

Now comes the fun part.  We’re going to record a macro!  It’s fairly easy.  We’ll record a few simple keystrokes, and then edit them for our use.  But first, select cells E6 through E8.  Then, from the top menu, click Tools, Macro, and Record New Macro:

hr241

We’ll name the macro HIDE_ROW and click OK:

hr071

Now, we’ll record keystrokes.  From the top menu, click Format, Row, and Hide:

hr081

Next, click the little Stop button that looks like this:

hr201

We’ll need to edit the macro that we just recorded, so from the top menu, select Tools, Macro, and Macros:

hr092

Then, select the macro named HIDE_ROW, and click the Edit button:

hr101

This will display, in Visual Basic,  the code for the macro that we recorded:

hr111

We’ll replace the existing code with this.  This will allow the macro to repeat for the appropriate number of times, and check to see if each row should be hidden or not:

hr121

When you’ve finished entering the new code, close the Visual Basic editor by clicking the X in the upper right corner.  Next, we’ll want to make it convenient to run the macro.  We’ll do this by creating a button.  From the top menu, click View, Toolbars, and Forms:

hr131

This will display the Forms toolbar.  Click on the second box down on the right hand side, which is the icon for the Button:

hr14

I placed the button over cells I2 through J3.  A dialog box will pop up that will prompt you to assign a macro to the button.  Select HIDE_ROW and click OK:

hr151

You can edit the name of the button by selecting the text and typing over it:

hr161

Now that we can hide the blank rows, we want to be able to display them again, or “unhide” them.  Let’s record another macro to do this.  First, select cells D6 through D40.  Remember the steps to record a macro?:

hr171

We’ll call this macro UNHIDE_ROWS:

hr181

Then, we’ll record the necessary keystrokes.  From the top menu, click Format, Row, and Unhide:

hr191

Next, stop the recording by clicking the following button:

hr20

We’ll want to create another button for the new macro, so let’s display the Forms toolbar one more time by clicking View, Toolbars, and Forms.  The Forms toolbar looks like this:

hr14

Select the Button icon, and place it over cells I6 through J7.  Assign the UNHIDE_ROWS macro to this button:

hr221

To finish, edit the text on the button:

hr231

Now, just click the buttons to run the macros.  Click the Hide Blank Rows button to hide the rows, and click the Unhide Blank Rows button to display the rows.   If you have any questions at all, please feel free to ask them in the comments.  I’ll get back to you promptly.

If you’ve found this post helpful, why not subscribe?  I’ll be posting more Excel tips in the future.  Subscribe

Print This Post Print This Post

Add to Del.cio.us RSS Feed Add to Technorati Favorites Stumble It! Digg It!
    www.sajithmr.com

Blog Traffic Exchange Related Posts
  • blog traffic exchangeGoing to a State College vs. a Private School Woo-hoo!  My son was accepted to a state college, and I couldn't be happier.  He was pretty excited, too.  It was his first choice, and has a great program for his major.  What makes it sweeter is the fact that this particular school had a record number of applicants for......
  • Age-Old Friends5 Things to Know About Social Security Photo by alan(ator) Yahoo! Finance recently published a guide to the five most common questions about Social Security benefits.  You can read the original article by clicking on the following link: What You Need to Know About Social Security Given all of the doubt that the entitlement program will......
  • Gentle GiantStanding on the Shoulders of Giants jurvetson I'm the office Excel guru, but I wasn't always comfortable with computers.  When I first starting working, we only had terminals that were hooked to a mainframe.  The only software we used was the mainframe program.  There was one PC, which was located in a small side office. ......
Blog Traffic Exchange Related Websites
  • 054MACROS Macros are advanced features that can speed up editing or formatting you may perform often in a Word document. They record sequences of menu selections that you choose so that a series of actions can be completed in one step. Recording A Macro To record a macro, follow these steps:......
  • Nikolay DavydenkoATP World Tour Finals: Nikolay Davydenko, Roger Federer, Juan Martin del Potro At the ATP World Finals, Roger Federer is now ranked 1 in the sport for the 5th time. He's one away from tying Pete Sampras' record of 6 number one spots. He won that trophy at the ATP World Tour Finals in London at the O2 Arena on Wednesday, but......
  • crimsontideEastern Division Florida Gators vs Western Division Alabama On December 5, 2009, the Eastern Division Florida Gators will face Western Division Alabama at the Georgia Dome in Atlanta, GA. This will be the 7th time since the championship began in 1992 that Alabama and Florida had met for this game. Of the 6 match ups to date,......

,

  1. No comments yet.
(will not be published)
CommentLuv Enabled

Subscribe without commenting