How can I format decimal places based on values? - excel

How can I format a number in Excel so that if it's a whole number with a decimal, it goes to the 10th, but if it doesn't have a whole number it goes to 100th?
Example:
0.08 -> 0.08
0.02 -> 0.02
1.66 -> 1.6
2.55 -> 2.5
2.89 - > 2.8
3.66 -> 3.6
5 -> 5
8 -> 8
Edit:
So far the formula provided by JvdV is working
=IF(A1-INT(A1)>0.1,ROUNDDOWN(A1,1),A1)
but needs to adjust for the following:
1) I need anything with 0.1 to contain its original value. With the current formula above this changes 0.15 to 0.10.
Example:
0.11 -> 0.11
0.15 -> 0.15
0.16 -> 0.16
0.19 -> 0.19
2) The formula needs to round up .01 to .09 only when they have a whole number in front of them.
Example:
0.01 - > 0.01
0.07 -> 0.07
0.09 -> 0.09
0.99 -> 0.99
1 -> 1
1.01 -> 1.1
1.02 -> 1.1
1.04 -> 1.1
1.05 -> 1.1
1.06 -> 1.1
1.07 -> 1.1
1.08 -> 1.1
1.09 -> 1.1
2.01 -> 2.1
2.04 -> 2.1
3.03 -> 3.1
3.09 -> 3.1
This is tricky because 0.01 should be formatted as 0.01. 0.02 should be formatted as 0.02, etc. Currently, the formula does this but does not round up for whole numbers.

I guess something like this could work:
=IF(A1-INT(A1)>0.1,ROUNDDOWN(A1,1),A1)
After your edited question, my edited answer would look like:
=IF(A1>1,IF(A1-INT(A1)>0.1,ROUNDDOWN(A1,1),ROUNDUP(A1,1)),A1)

You can use this formula:
=IF((INT(A1)=A1)=FALSE,IF(AND(A1>0,A1<1),TRUNC(A1,2),TRUNC(A1,1)),A1)

One more formula option:
=ROUNDDOWN(A1,(MOD(A1,1)>0)+(INT(A1)=0))

If you are okay with whole numbers also showing 1 decimal place (so 5 -> 5.0) then you can use a custom number format (there is also ways to use this method to hide the decimal on a whole number, but that would also require a using conditional formatting).
Select the range you want to format, then right click. Click on Format Cells, then select the Custom category.
Enter this in the 'Type' field:
[<1]0.00;0.0
Pros:
Doesn't require a helper column
Retains the original cell value. (If this is not desirable, then you can simply round or truncate the number. But this would require a formula [hence helper column] or possibly VBA)
Cons:
Will still format a whole number with .0. So 5 -> 5.0
You can find more information about custom format codes here: https://support.microsoft.com/en-us/office/number-format-codes-5026bbd6-04bc-48cd-bf33-80f18b4eae68

Related

Excel interpolate with hlookup

I have a table with x,y values. I want to interpolate for a given x1 value between the y values using the hllokup function also. I have found fomrulas for vlookup and xlookup but not for hlookup. I cannot use xlookup becaus eogf the verison of excel I use.
Example:
x-values 0.2 0.5 0.8 1.0 1.25 1.5 1.75 2.0 2.5 3.0 4.0
y-values 0.1 0.11 0.12 0.15 0.18 0.2 0.23 0.24 0.28 0.31 0.32
I need the y-value for x=1.1
I appreciate any help
There are various ways to interpolate: spline, polynomial, linear and so on.
I assume that you want linear interpolation between 2 x values.
In this case first of all, you need to find closest larger and closest lower x values:
Lower x:
=MAX(IF(B1:L1<B5,B1:L1))
Larger x:
=MIN(IF(B1:L1>B5,B1:L1))
Now need to find corresponding y's with HLOOKUP.
Lower x's y:
=HLOOKUP(A9,B1:L2,2,FALSE)
Larger x's y:
=HLOOKUP(B9,B1:L2,2,FALSE)
Now that you have all needed values you can write linear interpolation formula or you can use excel formula FORECAST. With 2 x's and 2 y's it will work as linear interpolation.
=FORECAST(B5,A11:B11,A9:B9)
Formula without using helper cells:
=FORECAST(B5,CHOOSE({1,2},HLOOKUP(MAX(IF(B1:L1<B5,B1:L1)),B1:L2,2,FALSE),HLOOKUP(MIN(IF(B1:L1>B5,B1:L1)),B1:L2,2,FALSE)),CHOOSE({1,2},MAX(IF(B1:L1<B5,B1:L1)),MIN(IF(B1:L1>B5,B1:L1))))
Result:

Find and replace values within ranges in excel

I have an array in excel that looks something like this, though much larger:
A B C D E F G
0 0 0 0 2.78 2.48 2.11
0 0 0 3.11 2.94 2.78 2.15
0 0 0 2.72 2.7 2.2 2.15
0 1.68 2.44 2.29 2.13 0 0
1.89 1.97 2.43 2.07 0 0 0
I'm trying to find all the values within an interval, then replace them with a different value according to a chart that looks like the one below. The values in the first row are the limits of an interval, and the second row represents the value I would like to insert into the array above. For example, the value in cell A5= 1.89. 1.89 falls between 1.8 and 1.9 so according to the chart below I would like the output to be 19.9192.
A B C D E F G ........>
1.6 1.7 1.8 1.9 2 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3 3.1 3.2
19.51 19.721 19.9192 20.1088 20.2903 20.4644 20.6318 20.7928 20.9484 21.0986 21.2441 21.3849 21.5214 21.654 21.7829 21.9083 22.0304
In terms of finding the values, I've tried the following :
=AND(A1 > Lower limit ,A1< Upper limit)
And using the Lower/ Upper limits as neighboring cells in the chart. For example,
=AND (A1> A10, A<A11)
which returns a value of either FALSE or TRUE depending of if the cell A1 is within the specified interval. I realize this isn't quite right but I'm not sure how to proceed. If its TRUE, I'm not sure how I can then insert the value I want based on the second chart - for example 19.9192 if the value was between 1.8 and 1.9, and if FALSE, I'm not sure how I can than move on to check the next interval and keep checking intervals until the right one is found an the cell is replaced appropriately. I think a loop might be needed, but I've not worked much with excel and haven't been able to get any syntax right to try it out.
A second look at your reference data, the format is in precision of 0.1. Whereas the input data is of 0.01 and 0.1. another condition is you take the lower bound of the difference. Thus, I would roundup the value 1st, then query the ref table using it. eg. 1.89 > convert to 1.8 > look for 1.8 pair value > retrieve value.
in I2 enter :
=IFERROR(INDEX($11:$11,MATCH(INT(A1*10)/10,$10:$10,0)),"") and drag it until O6.
+----[formula breakdown:]----+
round the number to 0.1 precision > INT(A1*10)/10
look for 1.8 position in the selection > match()
retrieve the value from the position identified > index()
hope that helps. (:

Matrix calculations using Excel INDIRECT

I would like to run a regression in excel using LINEST function combined with INDIRECT. I need to select the time series between dates and multiply Y and X with an Index before I run the regression.
Date | Y | X | Index
30/06/1990 1.21 2.20 -
30/09/1990 0.73 1.33 -
31/12/1990 -0.07 1.31 -
31/03/1991 1.64 0.80 1.00
30/06/1991 -0.14 0.61 1.00
30/09/1991 4.13 2.37 1.00
31/12/1991 0.71 0.78 1.00
31/03/1992 0.95 0.78 -
30/06/1992 1.61 0.78 -
By using the above data, I need to use the LINEST function from 31/03/1991 until 30/06/1992 but for dates only when the index is 1.
My idea was to use INDIRECT to specify the date range, multiply the LINEST( INDIRECT(Y)*INDIRECT(Index),INDIRECT(X)*INDIRECT(Index),0,0) but I have a error.
Thank you for your help
If the cells in the target range will always be consecutive, try...
H2, confirmed with CONTROL+SHIFT+ENTER:
=SMALL(IF($A$2:$A$10>=F2,IF($A$2:$A$10<=G2,IF($D$2:$D$10=1,ROW($A$2:$A$10)))),1)
I2, confirmed with CONTROL+SHIFT+ENTER:
=LARGE(IF($A$2:$A$10>=F2,IF($A$2:$A$10<=G2,IF($D$2:$D$10=1,ROW($A$2:$A$10)))),1)
J2, confirmed with just ENTER:
=LINEST(INDEX($B:$B,H2):INDEX($B:$B,I2),INDEX($C:$C,H2):INDEX($C:$C,I2),0,0)
Otherwise, try the following instead...
H2, confirmed with CONTROL+SHIFT+ENTER:
=LINEST(N(OFFSET($B$2:$B$10,SMALL(IF($A$2:$A$10>=F2,IF($A$2:$A$10<=G2,IF($D$2:$D$10=1,ROW($B$2:$B$10)-ROW($B$2)))),ROW(INDIRECT("1:"&SUM(IF($A$2:$A$10>=F2,IF($A$2:$A$10<=G2,IF($D$2:$D$10=1,1))))))),0,1)),N(OFFSET($C$2:$C$10,SMALL(IF($A$2:$A$10>=F2,IF($A$2:$A$10<=G2,IF($D$2:$D$10=1,ROW($C$2:$C$10)-ROW($C$2)))),ROW(INDIRECT("1:"&SUM(IF($A$2:$A$10>=F2,IF($A$2:$A$10<=G2,IF($D$2:$D$10=1,1))))))),0,1)),0,0)
However, for Excel 2010 and later, MODE.MULT can be used instead...
H2, confirmed with CONTROL+SHIFT+ENTER:
=LINEST(INDEX($B:$B,N(IF(1,MODE.MULT(IF($A$2:$A$10>=F2,IF($A$2:$A$10<=G2,IF($D$2:$D$10=1,ROW($A$2:$A$10)*{1,1}))))))),INDEX($C:$C,N(IF(1,MODE.MULT(IF($A$2:$A$10>=F2,IF($A$2:$A$10<=G2,IF($D$2:$D$10=1,ROW($A$2:$A$10)*{1,1}))))))),0,0)
Hope this helps!

Excel, more efficient formula for multiple IF ANDS

I have a spreadsheet that I am making that looks as follows:
Index Diff Exc Sym Sec Result Criteria Met
3.42 -2.07 0.86 0.92 1.83 1.95
-0.38 -2.93 0.87 0.23 -2.01 0.09
-2.67 -1.84 0.87 -2.49 -3.48 1.32
-0.65 -0.98 0.46 0.98 -2.01 0.00
-0.73 -2.79 -1.07 -2.15 -1.44 -0.10
0.15 2.33 -0.46 -0.66 3.17 0.38 0.38
0.90 -3.68 -0.72 -1.01 -1.36 1.69
0.68 -1.12 -0.36 0.73 -1.34 -0.29
-1.19 -1.70 -0.56 -1.31 1.45 0.49
-0.45 -0.69 -0.56 -1.22 0.00 -0.49
2.94 8.38 2.21 6.25 4.96 1.74
-1.04 7.36 2.59 3.00 2.17 2.97
1.21 1.73 3.05 1.48 3.56 0.77
-1.10 1.86 0.60 1.18 1.07 -0.49
-0.89 -3.19 -1.78 -2.24 -4.26 -0.81
-1.17 -3.44 0.11 -1.22 3.66 0.36
0.52 0.92 -1.02 0.38 1.96 -1.40 -1.40
-0.90 3.01 -0.86 0.62 0.97 -0.50 -0.50
2.78 1.46 0.00 0.47 1.95 0.84
Max Min
Index 2.00 -2.00
Diff 10.00 0.00
Exc 0.00 -10.00
Sym 10.00 -10.00
Sec 20.00 0.00
Under the headings Index, Diff, Exc, Sym, Sec, Result is all data, In the criteria met column i have a formula that checks if the prior headings fall within the Max and Min limits of the smaller table underneath, and if they do it posts the result, if they dont all fall within the Max and Min boundaries it leaves it blank. I did that by using this formula:
=IF(AND(A3<$B$24,A3>$C$24,B3<$B$25,B3>$C$25,C3<$B$26,C3>$C$26,D3<$B$27,D3>$C$27,E3<$B$28,E3>$C$28),F3,"")
copied down the criteria met column. It works perfectly fine for what I want it to achieve but as this spreadsheet grows and I add more columns it seems like it will be incredibly inefficient and prone to alot of human error. Is there a way to achieve the same results but by using a more efficient formula?
a picture for reference as well:
Try this array formula:
=IF(SUM((A3:E3<=TRANSPOSE($B$24:$B$28))*(A3:E3>= TRANSPOSE($C$24:$C$28)))=COLUMNS(A3:E3),F3,"")
Being an Array Formula it must be confirmed with Ctrl-Shift-Enter when exiting Edit mode. When done correctly Excel will automatically put {} around the formula to denote an array formula.
One caveat, the Columns to be compared need to be in the same order as the min/max rows.
Also, as has been stated by nearly everyone if the Min Max were transposed to rows from column it would alleviate needing to use a CSE array formula. The following would work:
=IF(SUMPRODUCT((A5:E5>=$A$3:$E$3)*(A5:E5<=$A$2:$E$2))=COLUMNS(A5:E5),F5,"")
Try something like this:
=IF(PRODUCT(IF(ROW>TRANSPOSE(MIN),1,0),IF(ROW<TRANSPOSE(MAX),1,0))=1,RESULT,"")
In this example, A11:E11 is a row of data with RESULT in F11 and the MAX and MIN criteria are in $B$27:$B$31 and $C$27:$C$31, respectively.
=IF(PRODUCT(IF(A11:E11>TRANSPOSE($C$27:$C$31),1,0),IF(A11:E11<TRANSPOSE($B$27:$B$31),1,0))=1,F11,"")
Enter the expression as an array formula (CTRL-SHIFT-ENTER). Make sure to use a relative reference for each row of data and absolute references for the MAX and MIN criteria. Then copy/paste to evaluate other rows. Modifying this formula to accommodate additional columns of data can be done by extending the ranges.
If you are permitted to transpose the range that holds the MAX and MIN criteria to the same orientation as the data, the TRANSPOSE functions in this solution can be eliminated and the spreadsheet layout will be cleaner.
Hope that helps
How about:
=IF(AND(A3=MEDIAN(A3,B$24,C$24),B3=MEDIAN(B3,B$25,$C$25),C3=MEDIAN(C3,B$26,$C$26‌​),D3=MEDIAN(D3,B$27,$C$27),E3=MEDIAN(E3,B$28,$C$28)),F3,"")
I would also move the criteria table into a separate tab to avoid that you have to adjust the loacation of the min/max values in your formula every time you add new rows to your data table.
Often in Excel, in order to simplify your formulas, you need to make your layout a little more complex. Without writing any vba, my approach would be ...
1) I would transpose your min / max layout and have them align with the columns of data.
2) Insert another sheet to hold intermediate calculations.
Here's Sheet1 ...
and here's Sheet2 ...
In Sheet2, cell B5 contains a typical formula ...
=IF(AND(Sheet1!B5<Sheet1!B$2,Sheet1!B5>Sheet1!B$3),1,0)
In Sheet2, cell G5 (and down) contains ...
=PRODUCT(B5:F5)
In Sheet1, cell H5 (and down) contains ...
=IF(Sheet2!G5=0,"",Sheet2!G5*G5)
It could omit the logic and you could make the sheet settings to not display 0 values.
If you add columns (e.g. insert columns between Sheet1 column F and G):
- add Max and Min values for new columns on Sheet1
- insert the same columns on Sheet2
- drag the formula from column F on sheet2 to the new columns on Sheet2
- verify the Product formula in the "Result" column on Sheet2 contains the new columns on Sheet2.
This method helps identify exactly what caused you to fail to meet your criteria.

Remove a constant from every cell of a column in an Excel equation

I have a column of coefficients and values
(Column A) (column B)
0.5 17.0
0.2 15.0
1.0 21.0
0.7 30.0
And I want to sum a constant and each coefficients in the column, e.g.
(1.0-0.5)*17.0 + (1.0-0.2)*15.0 + (1.0-1.0)*21.0 + (1.0-0.7)*30.0
Here, the constant is 1.0. What is the equation that is needed to achieve that? I have tried something like
SUMPRODUCT((1-A:A),B:B)
Without success.
How about:
=COUNT(A:A)-SUM(A:A)
This for for the constant 1. If the constant was 7 then use:
=7 * COUNT(A:A)-SUM(A:A)
EDIT#1:
Based on your Edit, I tried your proposed formula and it worked just fine!:

Resources