Default certain cells to 0 if blank

kiwikiki718

Board Regular
Joined
Apr 7, 2017
Messages
80
Office Version
  1. 365
Platform
  1. Windows
Hello is there a way to default certain fields to displsy 0 if cell is blank?

Ex. I have cells A1,A13,A23 that I want the defaulted value to be 0 if it is blank. Users can update the field but if they delete the data within the cells it would still default to 0.
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Copy and paste this macro into the worksheet code module. Do the following: right click the tab name for your sheet and click 'View Code'. Paste the macro into the empty code window that opens up. Close the code window to return to your sheet. Delete the data in one of the cells.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.CountLarge > 1 Then Exit Sub
    If Intersect(Target, Range("A1,A13,A23")) Is Nothing Then Exit Sub
    If Target = "" Then Target = 0
    Application.ScreenUpdating = False
End Sub
 
Upvote 1
Solution
Copy and paste this macro into the worksheet code module. Do the following: right click the tab name for your sheet and click 'View Code'. Paste the macro into the empty code window that opens up. Close the code window to return to your sheet. Delete the data in one of the cells.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.CountLarge > 1 Then Exit Sub
    If Intersect(Target, Range("A1,A13,A23")) Is Nothing Then Exit Sub
    If Target = "" Then Target = 0
    Application.ScreenUpdating = False
End Sub
Thank you this worked!
 
Upvote 0
hey mumps! this worked perfectly when i used the code for a slightly larger range and when a single cell was deleted, however, if more than 1 cell was selected and deleted, it wouldnt default back to '0'. is there an update that could be added to the code to make sure multiple cell deletion is covered? thanks!
 
Upvote 0
Try:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Application.ScreenUpdating = False
    If Intersect(Target, Range("A1,A13,A23")) Is Nothing Then Exit Sub
    Dim rng As Range
    For Each rng In Target
        If rng = "" Then rng = 0
    Next rng
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top