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
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.
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.
To see all of the blog posts in the Macro Monday series. Click on the link below