Solved

Search a range of data in Excel programmatically

Posted on 2014-02-04
2
161 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
macro for finding text and replacing 7 44
Moving Excel to AaaS 4 37
highlight duplicate entry 16 30
remove upper case characters in excel 4 19
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

895 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now