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

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

Gerald Higgins

Well-known Member
Joined
Mar 26, 2007
Messages
9,115
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 ?
 

Bruce52

New Member
Joined
Jun 19, 2014
Messages
2
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
 

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
20,919
Office Version
  1. 2016
  2. 2013
  3. 2007
Platform
  1. Windows
Does the cell hold a formula ?
If so, you can preded the formula with
Code:
=ABS(...the rest of your formula...)
 

Bruce52

New Member
Joined
Jun 19, 2014
Messages
2
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
 

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
20,919
Office Version
  1. 2016
  2. 2013
  3. 2007
Platform
  1. Windows
@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)
 

Forum statistics

Threads
1,141,734
Messages
5,708,162
Members
421,549
Latest member
Dtcfire

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
Top