How to contain formulas within a formula? - excel

I am trying to have formula return with a cell population with the first six characters of the look up cell given that the first two characters are 10.
See below for example.
=IF((LEFT(A3,2)=10), LEFT(A3,6), "")
As of right now, I keep getting a blank return no matter the look up cell's information.

This works for me:
=IF(LEFT(A3,2)="10",LEFT(A3,6),"")
LEFT returns a text string so you need ". You also don't need the double brackets.

You can aslo force the result of left() to be numerical,
=IF(LEFT(A3,2)*1=10,LEFT(A3,6),"")
I used *1, but +0 can work.
see
This can be convenient if you want to have the values like 10 or 15 etc in separate cells so you can just drag down.
See
You could use a cell to tell left() how many characters to collect or use the len() function.

Related

Get multiple substrings from cell by separator

I have multiple values in one cell that are ordered like this:
0#0#54232#5#123# ...
Now I want to put every single value in a separate cell. I tried working with SEARCH, FIND, LEFT, MID functions and so on, but everthing looks so fiddly and I can't get it to work. My idea was to look for every # in the text and get every substring in between. But with 20+ values in a single cell this gets very confusing by the end.
EDIT: The lengths of the values can vary
You can use FILTERXML to split the string (this solution will work on Office 365, since dynamic arrays need to be available):
=FILTERXML("<t><s>"&SUBSTITUTE(A1;"#";"</s><s>")&"</s></t>";"//s[string-length()>0]")
There has recently been a nice question here on Stackoverflow that I highly recommend to read:
Excel - Extract substring(s) from string using FILTERXML
You can use following formula (also in older excel versions):
=IFERROR(FILTERXML("<t><s>"&SUBSTITUTE($A$1;"#";"</s><s>")&"</s></t>";"//s["&ROW()&"]");"")
I've put your string in cell B3, and in cell C2, I've put following formula:
=LEFT(B3;FIND.SPEC("#";B3)-1)
In cell C3, I've put following formula:
=SUBSTITUTE(B3;C2&"#";"";1)
I've been dragging the whole thing to the right, and row 2 gave me the results you are looking for. (You might need to add some IfBlank() or IfError() but you understand what I mean)

Combining IFERROR and LEFT Formulas

I have a formula that looks like this
=IFERROR(B83,"OPEN")
So if a certain cell has an error it changes it to OPEN, but if it doesn't then it returns the values within that cell.
I am trying to make the cell also short the text that is being returned to 7 characters.
I made this formula:
IFERROR(B83,"OPEN"),AND(LEFT(B83,7))
However it does not work and instead returns an "NA".
Appreciate any help.
Try
IFERROR(LEFT(B83,7),"OPEN")
You need to put your desired result as the first argument of IFERROR.

Using Excel's SUMPRODUCT with SEARCH and ignoring blank cells

Suppose I have a row of cells that contain comma delimited strings like so:
I have figured out how to add the first character of each string using this formula:
=SUMPRODUCT(--(LEFT(C2:G2,SEARCH(",",C2:G2,1)-1)))
Now, I would like to extend the formula to the cell AA2, like so
=SUMPRODUCT(--(LEFT(C2:AA2,SEARCH(",",C2:AA2,1)-1)))
however, I cannot seem to get the it to ignore empty cells - it throws a #VALUE! error. I realize that I could simply update the formula each time I enter data in the subsequent cells, but that's not exactly efficient. I assume this throws an error because the SEARCH function returns a null value.
How might I get this to work?
You can try below modified formula:
=SUMPRODUCT(--(LEFT(C2:AA2&"0,",SEARCH(",",C2:AA2&"0,",1)-1)))
For right side you can use MID like below and check:
=SUMPRODUCT(--(MID(C2:AA2&".0",SEARCH(",",C2:AA2&",.0",1)+1,99)+0))
Your idea of searching for the comma is not needed if you only want to add the first character of each cell. Just grab the left-most single character.
Say we may have data from A1 to Z1 that may include some blank cells. Pick a cell and enter the array formula:
=SUM(IF(LEN(A1:Z1)>1,--LEFT(A1:Z1),0))
for example:
Array formulas must be entered with Ctrl + Shift + Enter rather than just the Enter key. If this is done correctly, the formula will appear with curly braces around it in the Formula Bar.

Excel Find the max length of characters after decimal in a given column

I'm trying to find a way to get the max number of characters after the decimal place in a given column. For example
I found this to get the max length in a column (using ctrl+shift+enter):
=MAX(LEN(A1:A5))
And this formula to get the number of characters after the decimal for a single cell:
=LEN(A1)-FIND(".",A1)
But I need to combine the two into a single formula so that I don't need another column of data. Is this possible without VBA?
Edit, one example I might encounter would be 99.999 vs 100.12 that I'd need to differentiate between and result in a length of 3 characters after the decimal.
If any of your data is the result of formulas, you may have some surprising results and need to use VBA. Otherwise, so long as the format is General, you can use
=MAX(LEN(A1:A5)-FIND(".",A1:A5&"."),0)
confirmed by holding down ctrl+shift while hitting enter
You can use Array Formulas in conjunction with some of your original suggestions to accomplish this. The formula in the example you provided would be:
{=MAX(IFERROR(LEN(B1:B3)-FIND(".",B1:B3),0))}
Some notes:
The "IFERROR" function is used to return a 0 to the MAX function if the "." is not found in the string
Array formulas can be entered into excel by entering the text "=MAX(IFERROR(LEN(B1:B3)-FIND(".",B1:B3),0))" into the formula bar and pressing ctrl+shift+enter (at which point the curly brackets will appear)
Applying this formula should yeild the following results for your sample inputs:
Sample Results

Check if Cells contain value exactly one time

I have five cells in Excel filled with ones and zeros and now I want to check if in those five cells there is zero exactly one time?
Example:
(11110) true
(11100) false
I need a regular Excel function without VB please.
Assuming the value is in cell A1, use this formula:
This is an array formula and must be confirmed with Ctrl+Shift+Enter.
=1=SUM(--(MID(A1,ROW(OFFSET($A$1,,,LEN(A1))),1)="0"))
Now copy the formula to point at the four other cells.
That's it.
Here is how it works...
Let's say that the value of cell A1 is 11101111
So, that's a total of eight characters. We can see that seven of them are 1s and one is a 0.
But how do we get Excel to see the same thing?
At the heart of the formula is the MID() function. We use it to break apart the value into eight separate characters.
Under normal usage, the MID() function just returns one substring of text from a larger string. You provide it the start position within the larger text and how long of a substring you want and it returns that substring.
For example: =MID(A1,3,3) would return 101 when referencing our A1.
And if we changed that to: =MID(A1,4,1) we would get 0.
But how can we have the MID() function completely decompose the value in A1 into discreet, single-character substrings. Remember that MID() normally just returns one substring. But in our case we want it to return eight substrings.
This is where the array-formula comes in. Remember that the 2nd parameter for MID() is the start position of the substring we want back. If we pass to that 2nd parameter an array of numbers instead of one single number, then MID() will perform its function once for each value in the array.
One way of specifying arrays in Excel is to use and array constant. So we could change our MID() example to this:
=MID(A1,{1;2;3;4;5;6;7;8},1)
And the result would look like this: {"1";"1";"1";"0";"1";"1";"1";"1"}.
Now if you just enter the above formula in a single cell, you will only see the first element, which is a one. But if you selected a range of eight vertical cells and then clicked on the Formula Bar at the top of the worksheet and entered the formula and then confirmed the array-way with Ctrl+Shift+Enter
... you would see these eight different values appear in the selected range.
Another way to see the full results, even from one cell, is to select the full formula inside the Formula Bar and then press the F9 key on the keyboard. The formula will be replaced with the results in the Formula Bar. You can use this technique to evaluate the formula or discreet parts of a larger formula. If you press Enter the results will stay in the Formula Bar. If you want the formula instead, press Esc.
The problem with using the array constant is that it only works correctly when the text in cell A1 is exactly eight characters long or less. If we want to handle longer text, then we need an array constant with more values. One way to side-step this issue is to use a formula to create an array on the fly that when evaluated would look like our array constant. We would design this formula to have precisely the same number of incrementing elements as there are characters in the text in cell A1.
That is precisely what this construct in the middle of our formula does:
ROW(OFFSET($A$1,,,LEN(A1))
Recall that when the MID() function is finished we are left with this array of results: {"1";"1";"1";"0";"1";"1";"1";"1"}
Now we apply a logical test to each element, asking is each of these equal to 0?
Here is how that would look:
{"1";"1";"1";"0";"1";"1";"1";"1"}=0
A new array result is returned that looks like this:
{FALSE;FALSE;FALSE;TRUE;FALSE;FALSE;FALSE;FALSE}
Notice that only the 4th element is TRUE, which of course is exactly right.
So that's progress.
Now we want to count how many elements are TRUE. We know the answer is one. But how can Excel figure this out?
It would be nice if our array were of numbers rather than these Boolean TRUE and FALSE values. An easy way to convert the array of Booleans to numbers is to use two minus signs in front of the array. When a TRUE value is turned negative it becomes a number; it becomes -1. When we hit it with the 2nd minus sign, that -1 becomes +1. And of course FALSE becomes 0.
So doing that would look like this:
--({FALSE;FALSE;FALSE;TRUE;FALSE;FALSE;FALSE;FALSE}=0)
And the result would look like this:
{0;0;0;1;0;0;0;0}
Notice that the array is now composed of numbers. When we first looked at the results from the MID() function, it looked like this: {"1";"1";"1";"0";"1";"1";"1";"1"} and those are not numbers; they are text. Notice all the quotation marks.
If you are following closely you may be wondering why we could not have used the two minus signs directly on the output from the MID() function to convert those text numbers into real numbers. Well, in truth we could have. But we want an array where the 1s represent the source zeroes and the 0s represent the source 1s. In other words, we want the inverse. Taking these extra steps gives us that.
We can now use SUM() on the array.
=SUM{0;0;0;1;0;0;0;0})
Since it is only summing 1s and 0s and the 1s represent the original source zeros, the result is the count of the source zeroes.
We perform one final test to see if the result is equal to one, because you want to know if the value in cell A1 has one and only one zero in it:
=1=SUM{0;0;0;1;0;0;0;0})
The result is TRUE.

Resources