# How to force a cell to only accept positive numbers?

#### Caula

##### New Member
Hi all,

I'm after a code or any other means of forcing a cell to only accept positive numbers depending on the value of anothet cell.

The problem is that sometimes I first input the value and then change the cell which determines that the value must be positive.

Any clues?

Thanks,

Caula from London

### Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Hi.

You can use Data Validation to force input of a positive number.

But this would not work if you sometimes input the value first, and THEN do something else which determines that the original number must be positive.

How would this work ideally ?

I wish I had run up on this post three years ago, but maybe someone else will need help with this. Assuming the active worksheet is Sheet1, C5 is the cell that determines that the value must be positive, and E5 contains the value, then the following vba code does what I think you asked for. As long as C5 is empty you can put anything you want in E5, but once C5 has a number in it E5 will contain the absolute value of that number. Of course, you could go with the absolute value of E5 instead if that is closer to what you need.

Open vba and choose Sheet1

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

If Range("C5").Value <> 0 Then
Range("E5").Value = Abs(Range("C5").Value)
End If

End Sub

Does the cell hold a formula ?
If so, you can preded the formula with
Code:
``=ABS(...the rest of your formula...)``

Neither cell holds a formula, it is all done with three short lines of vba code. If you put a formula into a cell and then type a number in it your formula will be overwritten and lost.

Cheers!

Bruce M

@Bruce
I was referring my reply to the OP. Easier to adjust a formula, if one is available.

even easier with one line of code
Code:
``If Range("C5").Value <> 0 Then Range("E5").Value = Abs(Range("C5").Value)``

Replies
4
Views
294
Replies
27
Views
2K
Replies
22
Views
593
Replies
1
Views
235
Replies
4
Views
166

1,221,410
Messages
6,159,723
Members
451,587
Latest member
srice33

### 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.

### Which adblocker are you using?

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

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