ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   0 (zero) auto delete (https://www.excelbanter.com/excel-discussion-misc-queries/213310-0-zero-auto-delete.html)

Ben in CA[_2_]

0 (zero) auto delete
 
Hi,

Is there any way that I can set a cell to automatically empty (delete the 0)
if someone enters a zero in the cell?

(Preferably with a macro)

(It's not for use by myself - I'd just hit the delete key instead of 0.)

Thanks for any suggestions.

Gary''s Student

0 (zero) auto delete
 
Put this event macro in the worksheet code area:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Value = 0 Then
Application.EnableEvents = False
Target.Clear
Application.EnableEvents = True
End If
End Sub


Because it is worksheet code, it is very easy to install and use:

1. right-click the tab name near the bottom of the window
2. select View Code - this brings up a VBE window
3. paste the stuff in and close the VBE window

If you save the workbook, the macro will be saved with it.

To remove the macro:

1. bring up the VBE windows as above
2. clear the code out
3. close the VBE window

To learn more about macros in general, see:

http://www.mvps.org/dmcritchie/excel/getstarted.htm

To learn more about Event Macros (worksheet code), see:

http://www.mvps.org/dmcritchie/excel/event.htm


--
Gary''s Student - gsnu200820


"Ben in CA" wrote:

Hi,

Is there any way that I can set a cell to automatically empty (delete the 0)
if someone enters a zero in the cell?

(Preferably with a macro)

(It's not for use by myself - I'd just hit the delete key instead of 0.)

Thanks for any suggestions.


Ben in CA[_2_]

0 (zero) auto delete
 
Thank you very much for the quick reply!

Is there a way I can set this to only do it for a certain range or cell -
instead of the whole worksheet?

E.g. - only for the cells C33 - E33

Ben

"Gary''s Student" wrote:

Put this event macro in the worksheet code area:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Value = 0 Then
Application.EnableEvents = False
Target.Clear
Application.EnableEvents = True
End If
End Sub


Because it is worksheet code, it is very easy to install and use:

1. right-click the tab name near the bottom of the window
2. select View Code - this brings up a VBE window
3. paste the stuff in and close the VBE window

If you save the workbook, the macro will be saved with it.

To remove the macro:

1. bring up the VBE windows as above
2. clear the code out
3. close the VBE window

To learn more about macros in general, see:

http://www.mvps.org/dmcritchie/excel/getstarted.htm

To learn more about Event Macros (worksheet code), see:

http://www.mvps.org/dmcritchie/excel/event.htm


--
Gary''s Student - gsnu200820


"Ben in CA" wrote:

Hi,

Is there any way that I can set a cell to automatically empty (delete the 0)
if someone enters a zero in the cell?

(Preferably with a macro)

(It's not for use by myself - I'd just hit the delete key instead of 0.)

Thanks for any suggestions.


Dave Peterson

0 (zero) auto delete
 
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Cells.Count 1 Then
Exit Sub 'one cell at a time
End If

If Intersect(Target, Me.Range("C33:E33")) Is Nothing Then
Exit Sub
End If

If Target.Value = 0 Then
Application.EnableEvents = False
Target.ClearContents 'save the formatting
Application.EnableEvents = True
End If
End Sub


Ben in CA wrote:

Thank you very much for the quick reply!

Is there a way I can set this to only do it for a certain range or cell -
instead of the whole worksheet?

E.g. - only for the cells C33 - E33

Ben

"Gary''s Student" wrote:

Put this event macro in the worksheet code area:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Value = 0 Then
Application.EnableEvents = False
Target.Clear
Application.EnableEvents = True
End If
End Sub


Because it is worksheet code, it is very easy to install and use:

1. right-click the tab name near the bottom of the window
2. select View Code - this brings up a VBE window
3. paste the stuff in and close the VBE window

If you save the workbook, the macro will be saved with it.

To remove the macro:

1. bring up the VBE windows as above
2. clear the code out
3. close the VBE window

To learn more about macros in general, see:

http://www.mvps.org/dmcritchie/excel/getstarted.htm

To learn more about Event Macros (worksheet code), see:

http://www.mvps.org/dmcritchie/excel/event.htm


--
Gary''s Student - gsnu200820


"Ben in CA" wrote:

Hi,

Is there any way that I can set a cell to automatically empty (delete the 0)
if someone enters a zero in the cell?

(Preferably with a macro)

(It's not for use by myself - I'd just hit the delete key instead of 0.)

Thanks for any suggestions.


--

Dave Peterson

Gary''s Student

0 (zero) auto delete
 
Add the two new lines:

Private Sub Worksheet_Change(ByVal Target As Range)
Set r = Range("C33:E33")
If Intersect(r, Target) Is Nothing Then Exit Sub
If Target.Value = 0 Then
Application.EnableEvents = False
Target.Clear
Application.EnableEvents = True
End If
End Sub
--
Gary''s Student - gsnu200820


"Ben in CA" wrote:

Thank you very much for the quick reply!

Is there a way I can set this to only do it for a certain range or cell -
instead of the whole worksheet?

E.g. - only for the cells C33 - E33

Ben

"Gary''s Student" wrote:

Put this event macro in the worksheet code area:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Value = 0 Then
Application.EnableEvents = False
Target.Clear
Application.EnableEvents = True
End If
End Sub


Because it is worksheet code, it is very easy to install and use:

1. right-click the tab name near the bottom of the window
2. select View Code - this brings up a VBE window
3. paste the stuff in and close the VBE window

If you save the workbook, the macro will be saved with it.

To remove the macro:

1. bring up the VBE windows as above
2. clear the code out
3. close the VBE window

To learn more about macros in general, see:

http://www.mvps.org/dmcritchie/excel/getstarted.htm

To learn more about Event Macros (worksheet code), see:

http://www.mvps.org/dmcritchie/excel/event.htm


--
Gary''s Student - gsnu200820


"Ben in CA" wrote:

Hi,

Is there any way that I can set a cell to automatically empty (delete the 0)
if someone enters a zero in the cell?

(Preferably with a macro)

(It's not for use by myself - I'd just hit the delete key instead of 0.)

Thanks for any suggestions.



All times are GMT +1. The time now is 09:31 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com