Using Excel's FIND Function to search for wildcard characters - excel

I would like to extract a substring to the right of a ** by using the FIND function. However, when I use the following formula,
=RIGHT(A2, FIND("~**", A2))
I receive the #VALUE error message. My understanding is that to search for wildcard characters in Excel strings, you must precede the wild-card with a ~ so I don't get why this formula does not work. Please see the attached image for the string and substring (intended) fields.String and Substring fields

Use the following:
=TRIM(RIGHT(A2,LEN(A2)- FIND("**", A2)-1))
You have to subtract Find() from the length of the string -1
Find will give the position of the first *
Right will count from end of string
It means you have to count tne number of character from the last to before *

Consider:
=MID(A2,FIND("**",A2)+2,9999)

Related

How to extract a string from 2 strings in Excel counting from the second string instead from the default left side

Here's a string:
Sample text here, EXTRACTTHIS(), and ignore the rest.
I want EXTRACTTHIS() to be extracted, so I used this simple formula:
=MID(LEFT(A5,FIND("()",A5)+1),FIND(" ",A5)+1,LEN(A5))
However I got this:
text here, EXTRACTTHIS()
Of course I can just mod it to be =MID(LEFT(A6,FIND("()",A6)+1),FIND(" ",A6)+10,LEN(A6)) to get EXTRACTTHIS().
But I want this formula to work with the whole column such as the following example:
I give you the next sample: WHAT_IF_THE_STRING_LENGTH_IS_DIFFERENT(), what to do?
The problem is that Excel counts from the left side of the parent string. I want Excel to count the 1st substring ' ' from the right side of the 2nd substring which is (). Is it doable?
In your examples, it appears you want to extract the substring that ends with ().
If that is not what you mean, please be more specific.
Try: =TRIM(RIGHT(SUBSTITUTE(LEFT(A1,FIND("()",A1)+1)," ",REPT(" ",99)),99))
Find the location of the (): FIND("()",A1)
Extract the portion of the string up to and including the ()
LEFT(A1, ... +1) => Sample text here, EXTRACTTHIS()
Then extract the last space separated substring from that
TRIM(RIGHT(SUBSTITUTE(... , " ",REPT(" ",99)),99))

How to extract a string from a cell from right to left using a specific character that occurs more than once?

Normally I would just use the RIGHT function in excel to split it by finding a specific character such as / and outputting the string that I want.
However, I am finding trouble extracting THISSTRING.txt from d/aaa/THISSTRING.txt. With only one instance of / I would just use a function such as =RIGHT(B17,LEN(B17) - FIND("/",B17))
Here's one way to get the rightmost:
=TRIM(RIGHT(SUBSTITUTE(A1,"/",REPT(" ",99)),99))
Objective: To return the rightmost sub-string from a target string after the last occurrence of a character which appears several times within the target string.
This formula:
=TRIM(RIGHT(SUBSTITUTE(A1,"/",REPT(" ",99)),99))
Provides the correct result under the following conditions:
The sub-string to retrieve does not have more than 99 characters.
The sub-string to retrieve does not contain more than one space character together.
Example: To retrieve a sub-string which is 123 characters long and contains the following characters 1 ABC XXX 123 XYZ.
Point 1 is easily solved by working with the length of the string instead of a fixed number:
=TRIM(RIGHT(SUBSTITUTE(A1,"/",REPT(" ",LEN(A1))), LEN(A1)))
However point 2 can't be overcome with the referred formula.
Proposed solution: The following formula returns the correct result regardless of the conditions mentioned above:
=RIGHT(A1, LEN(A1) - FIND( CHAR(12),
SUBSTITUTE(A1, "/", CHAR(12), LEN(A1) - LEN( SUBSTITUTE(A1, "/", "" )))))
Note: I used non-printable character 12 which is very unlikely to be found in excel, change as required.
Here's another way.....
=REPLACE(B17,1,LOOKUP(2^15,FIND("/",B17,ROW(INDIRECT("1:"&LEN(B17))))),"")
FIND generates an array of values, indicating the first position of a "/" in B17, but with the start point incrementing by 1 each time, which means that the last numeric value in that array is the positon of the last "/".
LOOKUP extracts that value from the array and we can use it in REPLACE function to replace all characters before and at that position with nothing, just leaving you with all characters after the last "/"
You'll get an error if there is no "/" in B17

MATCH function does not work with words that have wildcards (*)

I am trying to match HA24BB-3-1LL with HA24B*-3-1** in Excel. Another example is matching HA24FB-3-1LL with HA24F*-3-1**.
However, when I performed the regular match function, these could not be matched.
col A col B
1 HA24BB-3-1LL HA24F*-3-1**
2 HA24FB-3-1LL HA24B*-3-1**
What I tried:
=MATCH(A1,B:B,0)
It should return 2 but it returns #N/A.
May I know why?
I thought Excel match function works with wildcard. Is there a way to enable it?
You can match with wildcards, but the wildcards have to be in your lookup value (first position in the formula). If they are in the lookup array (second position in the formula) they are not wildcards, just literal *s in the cell values.
So you can find matches to strings like HA24B*-3-1** in your first column by using the formula: =MATCH(B1,A:A,0), but not the other way around, as your formula is set up.
Also, if you are looking for things that match HA24B[one character]-3-1[two characters] your search string should instead be HA24B?-3-1??. The * will match a string of any length, so it is redundant to put two of them at the end of your search string, and using them will also find you matches to strings like HA24Babcdedfghijklmnopqrstuvwxyz-3-1abcdefghijklmnopqrstuvwxyz. Which may be what you want, and if it is leave it as is (minus the second * at the end). The ? matches a single character, which I am assuming is what you are looking for since you used ** in your question.

MS Excel: remove number of characters after a specific character in a string

I have a column with values like
WI60P-14E64F5167E6-01138
or
WI60-34D03E185267-01051
etc....
i need to find the first occurrence of - till the second occurrence of - and remove the resulting character.
I am using this function =MID(A1,FIND("-",A1),13) which returns me -34D03E185267 or -14E64F5167E6 from the above strings.
But I want the output like WI60P-01138 or WI60-01051
Can anyone help me?
Assuming that the length of text between the two hyphens never varies, you can use your existing function, along with SUBSTITUTE to get the desired string, like so:
=SUBSTITUTE(A1,MID(A1,FIND("-",A1),13),"")
Alternately, you can use FIND to get the hyphen positions and again use SUBSTITUTE to get the replaced string, like so:
=SUBSTITUTE(A1,MID(A1,FIND("-",A1),FIND("-",A1,FIND("-",A1)+1)-FIND("-",A1)),"")
Notice that one of the FIND expressions takes 3 parameters - by passing FIND("-",A1) + 1 as the third parameter, we get the second occurence of '-' within the cell.
This will work for any position of the two hyphens:
=CONCATENATE(MID(A1;FIND("-";A1)+1;FIND("-";A1;FIND("-";A1)+1)-FIND("-";A1)-1))
Solution using multiple cells as support (with dynamic length of the string):
Put this in cell B1 to obtain the first part of the code:
=MID(A1,1,FIND("-",A1)-1)
Put this in cell C1 to obtain the last part of the code:
=MID(A1,FIND("-",A1)+1,LEN(A1))
Put this in cell D1 to concatenate both and get your result:
=concat(B1,C1)
Solution in a single cell (with dynamic length of the string)
Put this in cell B1 to get the same result as above :
=concat(MID(A1,1,FIND("-",A1)-1),MID(MID(A1,FIND("-",A1)+1,LEN(A1)),FIND("-",MID(A1,FIND("-",A1)+1,LEN(A1))),LEN(MID(A1,FIND("-",A1)+1,LEN(A1)))))

Excel Test Length of 1 field and divide if len>200

I have column B that I need to test the length to see if it is longer than 200 characters. If it is longer than 200 characters, I need it to go from right to left and find the occurrence of the semicolon ";" and split the field from the right of the semicolon into column C. Can this be done? Before I was having to do this with 4 columns and have reduced it to one column. Please advise to the best formula to do this.
=IF(LEN(B1)>200,MID(B1,SEARCH("#",SUBSTITUTE(B1,";","#",LEN(B1)-LEN(SUBSTITUTE(B1,";",""))))+1,LEN(B1)),"")
Explanation:
Remove all instances of the delimiter: SUBSTITUTE(B1,";","")
Subtract the length of (1) from the length of the entire string to get the number of occurrences of the delimiter: LEN(B1)-LEN([1])
Substitute the last occurrence of the delimiter with an #: SUBSTITUTE(B1,";","#",[2])
Find the location of the #: SEARCH("#",[3])
Get the substring of everything to the right of the # location: MID(B1, [4] +1,LEN(B1))
Add if condition to only process strings of length > 200: =IF(LEN(B1)>200,[5],"")
I searched the web for formulas for this and concluded that you either need a lot of nesting and a difficult to follow formula or a VBA function. I would suggest using a VBA function such as the one I have written below (FindLast) within a simple formula. Let me know if you need instructions on how to create this VBA formula:
Function FindLast(find_text As String, within_text As Range) As Double
Dim i As Integer
i = Len(within_text.Value) ' start at last character and work back
Do While Mid(within_text.Value, i, 1) <> find_text
i = i - 1
Loop
FindLast = i
End Function
You will then be able to use FindLast within a formula such as the below in C1:
=IF(LEN(B1)>200,MID(B1,FindLast(";",B1)+1,500),"")
UPDATE
The 500 in the above is just a long number I picked to mean the rest of the cell. If there may be more than 500 characters after the final ; then use a larger number. Unfortunately I don't think the MID function allows you to specify that you want to return the rest of the cell. I have put to return nothing if B1 is not >200 characters, let me know if this is not the requirement.

Resources