Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically filling in 0 and 0% in Excel blank cells

Tags:

excel-2007

I am trying to automatically enter 0 and 0% values in blank cells in Excel. I have a report in Excel that gets automatically filled up from SAS. After I save this report, I want the blank cells get automatically filled as 0 in numeric columns and 0% in percent columns.

What would the macro or VBA code be for that?

like image 795
Nupur Avatar asked Sep 29 '11 03:09

Nupur


People also ask

How do I automatically fill blank cells in Excel?

Press [Ctrl] + [Enter], and Excel will copy the respective formula to all blank cells in the selected range. This keyboard shortcut can be used as a quick way of filling a lot of cells or copying a formula into a range when it is entered, rather than copying it separately afterward.

How do I fill blank cells with null in Excel?

Alternatively, you can use "Find & Select", "Go to special", "Blanks". Then with all blank cells selected, write null and enter with [Ctrl]+[Enter]. This (ctrl+Enter) will enter the same data (null) in all selected cells.


1 Answers

If you just want to add 0 to a blank cell, there are several ways to do that - here's one using range A1:D10 as an example. Note that if a cell is formatted as percentage, the "%" is automatically appended to the 0. :

Sub test()

Dim cell As Range

For Each cell In Range("A1:D10")
    If Len(cell.Value) = 0 Then
        cell.Value = 0
    End If
Next

End Sub

Please note that if you are doing this on a large range of cells (actually it's good practice to do this all the time), you want to make sure you add Application.ScreenUpdating = False at the start of the code and Application.ScreenUpdating = True at the end. That will make it run much faster.

like image 129
aevanko Avatar answered Sep 28 '22 05:09

aevanko