Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I constrain a value to a range in excel?

Tags:

excel

vba

In C++ I would use boost::clamp for this. Basically I have some excel function

A1*B2+C3+D4

I want to do constrain it to +/- some number, call it X1. The obvious way is this:

MAX(-X1, MIN(X1, A1*B2+C3+D4))

But I want to be able to do this:

CLAMP(A1*B2+C3+D4, -X1, X1)

Does this or something similar exist? I'm just curious - obviously the workaround works, it's just ugly.

like image 213
quant Avatar asked Nov 24 '16 01:11

quant


People also ask

How do I create a fixed range in Excel?

1. Select the cell with the formula you want to make it constant. 2. In the Formula Bar, put the cursor in the cell which you want to make it constant, then press the F4 key.


1 Answers

This can be done using MEDIAN. MEDIAN picks the middle of the three values, thus effectively restricting the lower and upper limits.

For example say your minimum is 5 and your maximum is 10:

=MEDIAN(5,0,10) is 5

=MEDIAN(5,7,10) is 7

=MEDIAN(5,12,10) is 10

like image 139
Jasper Citi Avatar answered Nov 03 '22 16:11

Jasper Citi