Thank you so much MrKowz, this is exactly what I was looking for. Can you explain this to me? I want to know how it works.
I'll see what I can do.
First, we must understand the IF formula. The IF Formula is =IF(
condition,
value if true,
value if false)
In my formula, it can be broken down in the following fashion:
condition = AND(LEN(LEFT(A1,SEARCH("-",A1)-1))=3,MID(A1,SEARCH("-",A1)+1,2)="11")
value if true = ""
value if false = LEFT(A1,SEARCH("-",A1)-1)
Now, to elaborate on the string functions. Lets begin with
value if false. This is a common formula to return the left-most values of a string before a specified character. =LEFT(
cell,
length of string to return) will return the
LEFT length of string to return side of the
cell. Take a look at the below example for how the LEFT function returns values.
Excel 2013/2016 |
---|
|
---|
| A | B |
---|
1 | String | ABCDEFG |
---|
2 | | |
---|
3 | # of characters to return | |
---|
4 | 1 | A |
---|
5 | 2 | AB |
---|
6 | 3 | ABC |
---|
7 | 4 | ABCD |
---|
8 | 5 | ABCDE |
---|
9 | 6 | ABCDEF |
---|
10 | 7 | ABCDEFG |
---|
|
---|
We use the SEARCH(
find text,
within text) function to
SEARCH for the
find text value inside of the
within text string. SEARCH returns the character position of the
find text value inside of the
within text string. Again, see below for an example:
Excel 2013/2016 |
---|
|
---|
| A | B |
---|
1 | String | Position of "-" |
---|
2 | -23456 | 1 |
---|
3 | 1-3456 | 2 |
---|
4 | 12-456 | 3 |
---|
5 | 123-56 | 4 |
---|
6 | 1234-6 | 5 |
---|
7 | 12345- | 6 |
---|
|
---|
We can nest these functions together, but we must subtract 1 from the SEARCH result, since we want everything
before the
find text value.
Excel 2013/2016 |
---|
|
---|
| A | B | C | D |
---|
1 | String | Position of "-" | Left (including "-") | Left (excluding "-") |
---|
2 | -23456 | 1 | - | |
---|
3 | 1-3456 | 2 | 1- | 1 |
---|
4 | 12-456 | 3 | 12- | 12 |
---|
5 | 123-56 | 4 | 123- | 123 |
---|
6 | 1234-6 | 5 | 1234- | 1234 |
---|
7 | 12345- | 6 | 12345- | 12345 |
---|
|
---|
NOW to tackle the IF
condition: AND(LEN(LEFT(A1,SEARCH("-",A1)-1))=3,MID(A1,SEARCH("-",A1)+1,2)="11")
The AND(
condition1,
condition2, ...,
condition n) formula in this case has the following components:
condition 1 = LEN(LEFT(A1,SEARCH("-",A1)-1))=3
condition 2 = MID(A1,SEARCH("-",A1)+1,2)="11"
Lets start with
condition 1. Here, we're using the same logic as the LEFT/SEARCH explanation above... we're returning the actual string of everything found before the "-", but in this case, we're adding the LEN(
string) function around it to return the
LENgth of the string and testing to see if the length of that string is 3.
Excel 2013/2016 |
---|
|
---|
| A | B | C | D |
---|
1 | String | Left (excluding "-") | LEN | Length equals 3? |
---|
2 | -23456 | | 0 | FALSE |
---|
3 | 1-3456 | 1 | 1 | FALSE |
---|
4 | 12-456 | 12 | 2 | FALSE |
---|
5 | 123-56 | 123 | 3 | TRUE |
---|
6 | 1234-6 | 1234 | 4 | FALSE |
---|
7 | 12345- | 12345 | 5 | FALSE |
---|
|
---|
Now, on to
condition 2: MID(A1,SEARCH("-",A1)+1,2)="11". The MID function is just another string function. Its syntax is =MID(
text,
start number,
number of characters). Continuing with the examples I've been showing, lets say we want to return the
2 characters to the right of the "-". Note, we must add 1 to the result of SEARCH, because we want the returned value to begin
after the found string "-".
Excel 2013/2016 |
---|
|
---|
| A | B | C |
---|
1 | String | Position of "-" | 2 characters right of "-" |
---|
2 | -23456 | 1 | 23 |
---|
3 | 1-3456 | 2 | 34 |
---|
4 | 12-456 | 3 | 45 |
---|
5 | 123-56 | 4 | 56 |
---|
6 | 1234-6 | 5 | 6 |
---|
7 | 12345- | 6 | |
---|
|
---|
In the
condition 2, we're just testing to see if the 2 characters found after the "-" are equal to 11. If so,
condition 2 returns TRUE.
The AND function will only return TRUE if all conditions inside of it are true. So in this case, if the length of the characters found before the "-" is 3 and the two characters found after the "-" are "11", then we want a TRUE value returned, which will then fire the
true condition of the IF statement.
Phew, that was a lot of typing, but I hope it helps to put the entire thing into context!