change zero

tintin1012000

Board Regular
Joined
Apr 27, 2011
Messages
237
Hello,

I want to change a cell value from zero to 0.01 when zero is typed in.
I need this to happen automatically

Anyone know
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Try

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

If Intersect(Selection, Target) Is Nothing Then
    If Target.Value = 0 Then Target.Value = 0.1
End If

End Sub
 
Upvote 0
gaj 104 gave you good code, you may wnt to change the line - If Target.Value = 0 Then Target.Value = 0.1 to - If Target.Value = 0 Then Target.Value = 0.01
to get the desired 0.01 result.

Good job gaj 104 !
I'll make a note of your website
 
Upvote 0
Try

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

If Intersect(Selection, Target) Is Nothing Then
    If Target.Value = 0 Then Target.Value = 0.1
End If

End Sub
As I understand the requirement, that code is not very robust. Try each of these ..

1. Select a cell, type a 0 and instead of confirming with the Enter key, use the mouse to click the tick mark just to the left of the formula bar. (The same thing would result if the user has disabled the option to "After pressing Enter, move selection ..")

2. Select a group of cells, type a 0 and confirm with Ctrl+Enter, not just Enter.

3. Copy a zero cell (say from 1 above) and paste elsewhere.

4. Enter a 2 in a cell then re-select the cell and press F2 to edit. Now backspace to remove the 2 from the formula bar and press enter to confirm a blank (not 0) cell.


This would be my alternative ..
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim c As Range

    Application.EnableEvents = False
    For Each c In Target.Cells
        If c.Value = 0 And Len(c.Value) > 0 Then
            c.Value = 0.01
        End If
    Next c
    Application.EnableEvents = True
End Sub

BTW, in case you need to know how to implement this ..

a. Right click the sheet name tab and choose "View Code".

b. Copy and Paste the code above into the main right hand pane that opens at step a.

c. Close the Visual Basic window.

d. Try various data entry/edit/delete options.
 
Last edited:
Upvote 0
Peter SSs,

And what would an ignorant poor mortal soul like I do, without someone like you around to make sure I always keep my eyes wide shut?
 
Upvote 0

Forum statistics

Threads
1,224,567
Messages
6,179,571
Members
452,927
Latest member
whitfieldcraig

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