Solved

Search a range of data in Excel programmatically

Posted on 2014-02-04
2
176 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 33

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

726 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