[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 183
  • Last Modified:

Search a range of data in Excel programmatically

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
dbfromnewjersey
Asked:
dbfromnewjersey
2 Solutions
 
Steven HarrisPresidentCommented:
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
 
Rob HensonIT & Database AssistantCommented:
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now