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?
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With