How to force a cell to only accept positive numbers?

Caula

New Member
Joined
Aug 6, 2010
Messages
46
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

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
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 ?
 
Upvote 0
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
 
Upvote 0
Does the cell hold a formula ?
If so, you can preded the formula with
Code:
=ABS(...the rest of your formula...)
 
Upvote 0
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
 
Upvote 0
@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)
 
Upvote 0

Forum statistics

Threads
1,213,482
Messages
6,113,916
Members
448,533
Latest member
thietbibeboiwasaco

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