Solved

Search a range of data in Excel programmatically

Posted on 2014-02-04
2
165 Views
Last Modified: 2014-02-19
I have an Excel file that contains (for the most part) numeric data in columns B through F, rows 2 through 100 (row 1 contains headers). Some cells in that range are blank. I have already run a TRIM routine on the worksheet as well having manually deleted alls rows and columns outside of the used range. I need a routine that will search all cells in the used range and put a value of 0 (zero) in all blank cells in the range. Ideally, I need a routine that could be used for worksheets that have a few more columns and more rows. It just so happens that the file I am currently dealing with only has data in the columns and rows specified above. For example, the next file I have to work with may have data in columns B through Z and rows 2 through 500. Thank you.
0
Comment
Question by:dbfromnewjersey
2 Comments
 
LVL 18

Assisted Solution

by:Steven Harris
Steven Harris earned 250 total points
ID: 39832616
Insert the following code:

Sub FillEmptyBlankCellWithValue()
Dim cell As Range
Dim InputValue As String
On Error Resume Next
InputValue = "0"
    For Each cell In Selection
        If IsEmpty(cell) Then
            cell.Value = InputValue
        End If
    Next
End Sub

Open in new window


Select your range, then use Alt+F8 to bring up the Macro Selector and run the macro.

Example file attached.
FillBlank.xlsm
0
 
LVL 32

Accepted Solution

by:
Rob Henson earned 250 total points
ID: 39832672
Rather than selecting the area and then checking each and every cell.

Press F5, select Special, select Blanks and click OK. That then only selects blanks in the current used region.

The macro can then simply be:

For Each cell In Selection
        cell.Value = 0
Next cell

Alternatively without need for VBA, put a zero in the first blank cell and then copy it to the clipboard with Ctrl + C. Then use the F5, Special, Blanks selection and then Paste Special > Values.

Thanks
Rob H
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question