If Function, How do I proceed?

Jpack2552

New Member
Joined
Jul 28, 2017
Messages
13
Hi I have a quick question regarding the if function combined with the left function.

My data set looks like this. I only need the left side before the deliminator but with a filter.

88-9999
98-1120
102-1100
102-1200
104-1200
104-1165

I currently have this to separate the first half =LEFT(B1,FIND("-",B1)-1). I have difficulty trying to create a function to filter out certain numbers. For example. I want 102 and 104 but I do do not want- the 11's (102-1100,104-1100) I only want the 12's (104-1200, 102-1200). I only need this for the triple digits but still want to separate the first half.

Is this possible?
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Welcome to the forums!

Based on your example above, what is your desired output? I understand the condition of you don't want the 11's, you only want the 12's. Do you want to return blank if it finds an "11", but return the first 3 numbers when it finds a "12"?
 
Upvote 0
Thank you MrKowz.

88-9999 88
98-1120 98
102-1100
102-1200 102
104-1200 104
104-1165

Yes, sorry. I totally forgot about the output. I would like it to return blank if it is an 11 and if it is 12. I would like it to return the first three digits.

Thank you
 
Upvote 0
This seems to do the trick:


Excel 2013/2016
AB
188-999988
298-112098
3102-1100
4102-1200102
5104-1200104
6104-1165
Sheet17
Cell Formulas
RangeFormula
B1=IF(AND(LEN(LEFT(A1,SEARCH("-",A1)-1))=3,MID(A1,SEARCH("-",A1)+1,2)="11"),"",LEFT(A1,SEARCH("-",A1)-1))
 
Last edited:
Upvote 0
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.
 
Upvote 0
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
AB
1StringABCDEFG
2
3# of characters to return
41A
52AB
63ABC
74ABCD
85ABCDE
96ABCDEF
107ABCDEFG
Sheet19
Cell Formulas
RangeFormula
B4=LEFT($B$1,A4)


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
AB
1StringPosition of "-"
2-234561
31-34562
412-4563
5123-564
61234-65
712345-6
Sheet19
Cell Formulas
RangeFormula
B2=SEARCH("-",A2)


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
ABCD
1StringPosition of "-"Left (including "-")Left (excluding "-")
2-234561- 
31-345621-1
412-456312-12
5123-564123-123
61234-651234-1234
712345-612345-12345
Sheet19
Cell Formulas
RangeFormula
B2=SEARCH("-",A2)
C2=LEFT(A2,B2)
D2=LEFT(A2,B2-1)



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
ABCD
1StringLeft (excluding "-")LENLength equals 3?
2-23456 0FALSE
31-345611FALSE
412-456122FALSE
5123-561233TRUE
61234-612344FALSE
712345-123455FALSE
Sheet19
Cell Formulas
RangeFormula
B2=LEFT(A2,SEARCH("-",A2)-1)
C2=LEN(B2)
D2=C2=3


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
ABC
1StringPosition of "-"2 characters right of "-"
2-23456123
31-3456234
412-456345
5123-56456
61234-656
712345-6
Sheet19
Cell Formulas
RangeFormula
B2=SEARCH("-",A2)
C2=MID(A2,B2+1,2)


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!
 
Upvote 0
JPack - your inbox was full, but I think my response would be helpful to anyone else who happens across this:

To your question of good resources to learn, check out Hiker95's response on this thread: Gratitude

He outlines a TON of resources that you may find useful.

And I appreciate the feedback! One thing that has helped me get to the point where I am now is to always practice, always research, and always try to figure out how something works rather than just accepting the fact that it works. You already have the spark of curiosity, so never let that die out and continue learning. Excel can do some really awesome things once you get into the crazy coding world that is VBA. :)
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,849
Members
452,361
Latest member
d3ad3y3

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
Back
Top