Data entry cell must contain a zero

leopardhawk

Well-known Member
Joined
May 31, 2007
Messages
611
Office Version
  1. 2016
Platform
  1. Windows
Hello forum friends, my worksheet has one particular cell where the user will be entering a number. Is it possible that if the user clears the cell, deletes whatever number is in it, that the cell will ALWAYS contain at a minimum, a zero (0)...?? I have another cell on the worksheet that is doing a COUNT and this 'blank' cell is giving me an error...

Thanks!
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Right-click on the sheet tab name at the bottom of the page, select "View Code", and paste this code in the VB Editor window:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.CountLarge > 1 Then Exit Sub
   
    If Target.Address(0, 0) = "B4" Then
        If Len(Target) = 0 Then
            Application.EnableEvents = False
            Target = 0
            Application.EnableEvents = True
        End If
    End If

End Sub
Just change the "B4" to whatever cell you want to apply this to, and it will run automatically when that cell is changed.
 
Upvote 0
Here is a better version that will handle the case if they clear multiple cells at one time (the first code only works if they are clearing exactly that one cell):
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim cell As Range
    Dim rng As Range
    
'   Set cell that you want to watch
    Set cell = Range("B4")
    
    Set rng = Intersect(Target, cell)
    
    If Not rng Is Nothing Then
        If Len(cell) = 0 Then
            Application.EnableEvents = False
            cell = 0
            Application.EnableEvents = True
        End If
    End If

End Sub
 
Upvote 0
@Joe4 Thank you. Is it possible to do this without using VBA? If I add the code, I think I have to save the file as macro-enabled and the users will also then have to enable macros when they open the workbook? I am trying to make this as simple as possible for the majority of the target audience who may not be comfortable enabling anything. Thanks!
 
Upvote 0
No, formulas can only update the cells that they are located in, and you can only have either a hard-coded value or a formula in a cell, never both at the same time.
So you cannot do this without VBA.

What you can do is have them update some other cell, like let's say A4.
Then have this formula in cell B4:
=A4
and use B4 instead of A4 in your count.

If A4 is empty, B4 will return 0. You can even lock B4 so they cannot touch that formula.
 
Upvote 0
@Joe4 Thank you! I think that will work just fine for what I am trying to do. I can also 'hide' the results in B4 with ;;; in a custom format. Thanks again!
 
Upvote 0
Thank you! I think that will work just fine for what I am trying to do. I can also 'hide' the results in B4 with ;;; in a custom format. Thanks again!
You are welcome.
 
Upvote 0

Forum statistics

Threads
1,215,695
Messages
6,126,263
Members
449,307
Latest member
Andile

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