Restrict an input of zero

Deuce Bucksman

Board Regular
Joined
Jan 17, 2011
Messages
102
Hi-

I'm trying to write a macro that will not allow a number to be zero. I have a macro that references a cell that if it is input to zero will create a loop that goes on forever. The thing is this cell will never have to be zero, so is there a way that I can write a macro that if this cell is zero, it gets reset to its previous value?
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Use data validation (in the data tab of the ribbon). Whole number, then set the dropdown to "not equal to".

Not sure the VBA for it. but do it with the recorder on and then see what it recorded.
 
Upvote 0
Maybe ...

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Static v           As Variant

    If Target.Address = "$A$1" Then
        If VarType(Target.Value2) = vbDouble And _
           Target.Value2 = 0 Then
            Application.EnableEvents = False
            Target.Value = v
            Application.EnableEvents = True
        End If
        v = Target.Value
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,219,162
Messages
6,146,659
Members
450,706
Latest member
LGVBPP

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