Discussion Locked

Excel 97/2000 macro question

0 helpful
5 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I have a user that needs to automagically convert any negative numbers in her Excel spreadsheet to zero. How can she do this? Is there a setting to change or would it be a macro? Any ideas on how to write the macro?

tia,
mrountre@electricities.org

5 replies

by don christner

I would handle this by adding another column next to the column that may have negative numbers in it. Let's call the negative column A and add column B.
Next in cell B1, add this formula, IF(A1<0,0,A1). This says that if the value of A1 is less than 0, then B1=0, or else B1=A1. Then run your other calculations using column B instead of A. Of course you will copy the formula from B1 to all cells in column B.

Don

by dklippert

Right click on the Sheet tab and choose View code.
Paste this code in to make the changes anywhere on that particular spreadsheet.

Private Sub Worksheet_Change(ByVal Target As Range)
If Target(1).Value < 0 Then
Target(1).Value = 0
EndIf
End Sub

If you want to restrict the negative number conversion to a particular Range, edit this code. (Borrowed from www.CPearson .com)

Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
If Not Application.Intersect(Target, Range("A1:A10")) Is Nothing Then
If Target(1).Value < 0 Then
Target(1).Value = 0
End If
End If
Application.EnableEvents = True
End Sub

This discussion is closed to new replies.