Use An Excel Macro To Fill Blank Cells – Macro Monday


Hello Excellers, a warm welcome back to #macromondays. I want to share with you a VBA or macro solution to an Excel process that you probably think already saves you a tonne of time. Do you have Excel data given to you either by another Excel user or extracted from a business system you use that you have have to fill in all of the blank cells in your data range with the value above?. Probably our data probably will look something like this below.

The GoTo Special Method

You probably already use the method below as well to fill the blanks from above.

  • Select your data range
  • F5 | Goto Special Blanks
  • Type the = equals sign | Press CTRL | Hit Enter
  • Select data range | Copy | Paste Special Value

So, while this is a super method, how about creating a macro that just does that for you. Just select your data range then run the macro. Even faster!

 

Back To The Macro…..What Does The Macro Do?

OK, let’s get working on some VBA code which will look at each cell in the range we select, if the cell is blank then it will be filled with the value of the cell directly above it.

How Does The Macro Work?

First, we need to create a new module to save this Macro into. Insert a module by opening Visual Basic by hitting ALT+F11, or select the Developer Tab and in the Code Group Select Visual Basic. You can now hit Insert Module. I have chosen to store this in my Personal Macro Workbook so I can use with any workbook I want to.

If you want to read more about the Personal Macro Workbook then you can read my blog posts below

Macro Mondays -Creating and Updating Your Personal Macro Workbook

Macro Mondays – Create A Shortcut To Your Personal Excel Macro Workbook

So, if you want to save the macro in the workbook you are working on the insert the module into the current workbook. If you do this the macro will only be available in that particular Excel workbook.

Step 1. We need to declare a couple of variables. This just means that Excel creates a memory container to store these values.

Dim c As Range (cel within the range)

Dim rng As Range (this is the range selected by the user)

Step 2. This is where we set the range to be evaluated which is collected by the use of an Input Box, and we specify that the type is 8. Below is the types that can be used with an Input Box.

An input box that can accept both text and numbers, set Type to 1 + 2.

Value Meaning
0 A formula
1 A number
2 Text (a string)
4 A logical value (True or False)
8 A cell reference, as a Range object
16 An error value, such as #N/A
64 An array of values

For more information about the Input Box feel free to read my blog post below

Excel VBA InputBox Function – The Basics

Step 3. First of all, Excel starts to evaluate each cell in the range set by the user to see if it is blank. If the cell is blank the Excel fills the cell with the value of the cell above using the OFFSET property. -1 represents 1 row up (the cell above) and 0 is the current column. Excel repeats this process is repeated until all cells have been evaluated.

Step 4. So, once all of the cells have been evaluated then the routine ends.

Step 5. Test it! This is my favourite part.

 

Give this one a go and compare how fast it is versus to the Goto Special method.

What Next? Want More Excel Tips?. Then Read On…

If you want more Excel and VBA tips then sign up for my Monthly Newsletter where I share 3 Tips on the first Wednesday of the month and receive my free Ebook, 30 Excel Tips.

1

To see all of the blog posts in the Macro Monday series. Click on the link below

How To Excel At Excel – Macro Mondays Blog Posts.

 

 

More Excel Tips_New1

Learn Excel With Our Excel Online Courses

ExcelRescue.net

Barbara

Barbara is an experienced data analyst with over 20 years of experience. Author of the How To Excel At Excel Top 50 Excel tips Ebook, YouTuber and Excel Blogger.

Recent Posts