Excel: referencing a portion of file name only - excel

Please help, i'm looking to reference a portion of my file name in an excel cell, see below example:
File name: 000_XYZ_ABC_DEF ; I need to reference the ABC portion (but this won't be limited to only 3 letters as this varies) - is there a formula I could use that would cater for the variance as well as only referencing this portion?
thank you

In B1, formula copied down :
=TRIM(LEFT(RIGHT(SUBSTITUTE(A1,"_",REPT(" ",30)),60),30))

Well, very brutal with MID() and FIND() but works:
Basic explanation is that MID() takes the characters from a string from the start points and end point you define.
The use of find() is to locate the second underscore which is the start point (plus 1 though) and the position of the 3rd underscore which with the position of the second gives the number of characters to return.
It will be good practice to take it all apart.
Edit: I have added the complete function, so that you can copy it directly:
=MID(A1,FIND("_",A1,FIND("_",A1,1)+1)+1,FIND("_",A1,FIND("_",A1,FIND("_",A1,1)+1)+1)-FIND("_",A1,FIND("_",A1,1)+1)-1)
In your comment you have not included in your function the "_" which is the precise bits of the text FIND() is looking for. Compare mine to yours from your comment:
=MID(A1,FIND("",A1,FIND("",A1,1)+1)+1,FIND("",A1,FIND("",A1,FIND("",A1,1)+1)+1)-FIND("",A1,1)+1)-1

Related

How can I extract an instagram username from a hyperlink to a neighboring column and keep the hyperlink on google sheets?

I'm tasked with going through a long column of instagram profile URLs and creating a new adjacent column comprised of just their usernames. I could theoretically go through the list individually, copy-pasting the part between ".com/" and the last "/" and then hyperlinking each of them, but I feel like there might be a faster way.
I've experimented with formulas trying to extract only the username but to no avail. I also realized formula cells can't be hyperlinked, so I would also need a solution for that. Here what I was trying so far:
Here the input and expected output:
URL
User Name
http://instgram.com/stack_overflow/
stack_overflow
http://instgram.com/stackoverflowing/
stackoverflowing
http://instgram.com/stackoverflowthestack/
stackoverflowthestack
http://instgram.com/stackoverflowingstacks/
stackoverflowingstacks
The end result should look like User Name Column and be adjustable for any length of usernames (slashes and spaces among other special characters cannot be used in instagram usernames).
Also, I'm unsure why my google docs takes semi-columns instead of commas as I'm used to with Excel, but it is what it is.
Figuring this out would save me loads of time in the long-run and I would be very appreciative.
If you can use TEXTAFTER (Office Insider Beta only, Windows: 2203 (Build 15104), Mac: 16.60 (220304)). You don't have to deal with LEFT/RIGHT/FIND functions. On B2 cell you enter and expand down the following formula:
=SUBSTITUTE(TEXTAFTER(A1,"/",-2),"/","")
Here is the output:
You can replace SUBSTITUTE as follow via TEXTBEFORE to remove the last /:
=TEXTBEFORE(TEXTAFTER(A1,"/",-2),"/")
Explanation
The main idea is that:
TEXTAFTER(text,delimiter,[instance_num], [match_mode],
[match_end], [if_not_found])
allows to search backward, using the third input argument: instance_num (with a negative number). We are interested in the penultimate occurrence of /, therefore this input argument would be -2.
Alternative Solution
If such functions are not available for your excel version the following works using MID(text, start_num, num_chars) function:
=LET(url, A1, length, LEN(url), count, length-LEN(SUBSTITUTE(url,"/",""))-1,
startPos, FIND(" ",SUBSTITUTE(url,"/"," ",count))+1, numChars, length-startPos,
MID(url,startPos, numChars))
Note: We use LET to avoid repeating the same calculation and also to make it easier to understand. Without LET it's a bigger formula but it works too:
=MID(A1, FIND(" ",SUBSTITUTE(A1,"/"," ",
LEN(A1)-LEN(SUBSTITUTE(A1,"/",""))-1))+1, LEN(A1) -
(FIND(" ",SUBSTITUTE(A1,"/"," ",LEN(A1)-LEN(SUBSTITUTE(A1,"/",""))-1))+1))
we are looking for the penultimate / so count name achieves that:
count, length-LEN(SUBSTITUTE(url,"/",""))-1
The following formula:
startPos, FIND(" ",SUBSTITUTE(url,"/"," ",count))+1
is a way to replace just the penultimate / by space ( ). URLs don't have spaces so it is a good replacement. Then finding the position of such space plus 1 will give us the starting position required by MID. Having the starting position and the length of the URL we can calculate the number of characters (numChars), so we can finally invoke MID with the required input arguments.
Note: The approach you tried in your question, relies on specific pattern of the URL (ending with .com) the above approaches don't have such constraint, so you can use the following URL: https://www.redcross.org/ and it works.

How to substring in Excel between different characters?

first-time poster so please bear with me. I am trying to convince Excel to do a substring and failing miserably. The task is simple enough on the surface of it, extract text that's between a fixed set of chars (+, -, * and /), basically mathematical operators.
My input string looks like this:
A+B+C+D
Now, if my string looks like that, or like A-B-C-D, all is good, I can use this and it works (not my code, found on https://exceljet.net/formula/split-text-with-delimiter and modified to suit my needs:
First text: TRIM(MID(SUBSTITUTE($A2,"+",REPT(" ",LEN($A2))),0*LEN($A2)+1,LEN($A2)))
Second: TRIM(MID(SUBSTITUTE($A2,"+",REPT(" ",LEN($A2))),1*LEN($A2)+1,LEN($A2)))
Third: TRIM(MID(SUBSTITUTE($A2,"+",REPT(" ",LEN($A2))),2*LEN($A2)+1,LEN($A2)))
Forth: TRIM(MID(SUBSTITUTE($A2,"+",REPT(" ",LEN($A2))),3*LEN($A2)+1,LEN($A2)))
And all is good, until I have a string like: A-B+C-D or wahtever combo, basically not all the same char.
I tried using Find and Search in different configurations, but I always come to the same problem:
Using substitute gives me the n'th occurance and that's no good as - may be my second symbol or third
Can't dynamically and accurately calculate the length for MID, as it does Nr. of chars, not "until"
I can't use VB script for security reasons, so I am stuck trying to use Excel formulas.
It HAS to be one formula, as in the end, it's part of a bigger formula that's something like this:
CONCATENATE(IF(ISNUMBER(A),A,VLOOKUP(A)),IF(ISNUMBER(A),A,VLOOKUP(A)),IF(ISNUMBER(A),A,VLOOKUP(A)),IF(ISNUMBER(A),A,VLOOKUP(A)))
So I have the input in a cell and my result has to do all the processing in an adjacent cell.
Thank you in advance, at whit's end over here.
You can try FILTERXML() function.
=TRANSPOSE(FILTERXML("<t><s>"&SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1,"+","|"),"-","|"),"*","|"),"/","|"),"|","</s><s>")&"</s></t>","//s"))
If you are not on Excel365 then try below formula.
=FILTERXML("<t><s>"&SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE($A1,"+","|"),"-","|"),"*","|"),"/","|"),"|","</s><s>")&"</s></t>","//s[" & COLUMN(A1) &"]")
To learn FILTERXML() go through this article from #JvdV
For lower versions of Excel following formula would work by copying across as much as needed:
=TRIM(MID(SUBSTITUTE(" "&SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE($A1,"+"," "),"-"," "),"/"," "),"*"," ")," ",REPT(" ",99)),99*COLUMNS($A1:A1),99))
which is fairly similar to what has been posted on Exceljet site.

Find function works weird

Well... It's really weird, but let me explain.
I am trying to get the country string from the A1. So i should get the position of second # and after that i can use MID and get the country. But, well.. Please check out.
Here is the situation:
A1: "Abergwili# Wales# United Kingdom"
B1: =FIND("#";A1;1) > Result: 10 >> Correct!
C1: =FIND("#";A1;B1) > Result: 10 >> What??? It should output 7! I really don't get it!
Any help?
The FIND function in Excel uses the last argument as the string index to start searching from. If that index matches the target string, it will return that index again. There are a few options:
You could restructure your current FIND function to start at the character after B1's result, which will produce the formula =FIND("#",A1,B1+1). To get the position relative to B1, you could subtract the value of B1, which creates =FIND("#",A1,B1+1)-B1. (This is similar to JvdV's answer)
You could explicitly search through the remaining substring after the first match using the RIGHT function, which transforms the original function to =FIND("#",RIGHT(A1, LEN(A1)-B1)), which automatically returns the result relative to the last match. To get the absolute result, you could add the value of B1 back in, creating =FIND("#",RIGHT(A1, LEN(A1)-B1))+B1.
Good luck!
(Side note - You probably want commas instead of semicolons, unless your Excel is set up to work with them. My Excel complained mightily when testing with ; characters)

retrieve part of the info in a cell in EXCEL

I vaguely remember that it is possible to parse the data in a cell and keep only part of the data after setting up certain conditions. But I can't remember what exact commands to use. Any help/suggestion?
For example, A1 contains the following info
0/1:47,45:92:99:1319,0,1320
Is there a way to pick up, say, 0/1 or 1319,0,1320 and remove the rest unchosen data?
I know I can do text-to-column and set the delimiter, followed by manually removing the "un-needed" data, but my EXCEL spreadsheet contains 100 columns X 500000 rows with each cell looking similar to the data above, so I am afraid EXCEL may crash before finishing the work. (have been trying with LEFT, LEN, RIGHT, MID, but none seems to work the way I had hoped)
Any suggestion will be greatly appreciated.
I think what you are looking for is combination of find and mid, but you'll have to work out exactly how you want to split your string:
A1 = 0/1:47,45:92:99:1319,0,1320 //your number
B1 = Find(“:“,A1) //location of first ":" symbol
C1 = LEN(A1) - B1 //character count to copy ( possibly requires +1 or -1 after B1.
=Left(A1,B1) //left of your symbol
=Mid(A1,B1+1,C1) //right size from your symbol (you can also replace C1 with better defined number to extract only 1 portion
//You can also nest the statements to save space, but usually at cost of processing quantity increase
This is the concept, you will probably need to do it in multiple cells to split a string as long as yours. For multiple splits you probably want to replicate this command to target the result of previous right/mid command.
That way, you will get cell result sequence like:
0/1:47,45:92:99:1319,0,1320; 47,45:92:99:1319,0,1320; 92:99:1319,0,1320; 99:1319,0,1320......
From each of those you can retrieve left side of the string up to ":" to get each portion of a string.
If you are working with a large table you probably want to look into VB scripting. To my knowledge there is no single excel command that can take 1 cell and split it into multiple ones.
Let me try to help you about this, I am not a professional so you may face some problems. First of all my solution contains 2 columns to be added to the source column as you can see below. However you can improve formulas with this principle.
Column B Formula:
=LEFT(A2,FIND(":",A2,1)-1)
Column C Formula:
=RIGHT(A2,LEN(A2)-FIND("|",SUBSTITUTE(A2,":","|",LEN(A2)-LEN(SUBSTITUTE(A2,":","")))))
Given you statement of having 100x columns I imagine in some instances you are needing to isolate characters in the middle of your string, thus Left and Right may not always work. However, where possible use them where you can.
Assuming your string is in cell F2: 0/1:47,45:92:99:1319,0,1320
=LEFT(F2,3)
This returns 0/1 which are the first 3 characters in the string counting from the left. Likewise, Right functions similarly:
=RIGHT(F2,4)
This returns 1320, returning the 4 characters starting from the right.
You can use a combination of Mid and Find to dynamically find characters or strings based off of defined characters. Here are a few examples of ways to dynamically isloate values in your string. Keep in mind the key to these examples is the nested Find formula, where the inner most Find is the first character to start at in the string.
1) Return 2 characters after the second : character
In cell F2 I need to isolate the "92":
=MID(F2,FIND(":",F2,FIND(":",F2)+1)+1,2)
The inner most Find locates the first : in the string (4 characters in). We add the +1 to move to the 5th character (moving beyond the first : so the second Find will not see it) and move to the next Find which starts looking for : again from that character. This second Find returns 10, as the second : is the 10th character in the string. The Mid formula takes over here. The formula is saying, Starting at the 10th character return the following 2 characters. Returning two characters is dictated by the 2 at the end of the formula (the last part of the Mid formula).
2) In this case I need to find the 2 characters after the 3rd : in the string. In this case "99":
=MID(F2,FIND(":",F2,FIND(":",F2,FIND(":",F2)+1)+1)+1,2)
You can see we have simply added one more nested Find to the formula in example 1.

How do I get the last character of a string using an Excel function?

How do I get the last character of a string using an Excel function?
No need to apologize for asking a question! Try using the RIGHT function. It returns the last n characters of a string.
=RIGHT(A1, 1)
=RIGHT(A1)
is quite sufficient (where the string is contained in A1).
Similar in nature to LEFT, Excel's RIGHT function extracts a substring from a string starting from the right-most character:
SYNTAX
RIGHT( text, [number_of_characters] )
Parameters or Arguments
text
The string that you wish to extract from.
number_of_characters
Optional. It indicates the number of characters that you wish to extract starting from the right-most character. If this parameter is omitted, only 1 character is returned.
Applies To
Excel 2016, Excel 2013, Excel 2011 for Mac, Excel 2010, Excel 2007, Excel 2003, Excel XP, Excel 2000
Since number_of_characters is optional and defaults to 1 it is not required in this case.
However, there have been many issues with trailing spaces and if this is a risk for the last visible character (in general):
=RIGHT(TRIM(A1))
might be preferred.
Looks like the answer above was a little incomplete try the following:-
=RIGHT(A2,(LEN(A2)-(LEN(A2)-1)))
Obviously, this is for cell A2...
What this does is uses a combination of Right and Len - Len is the length of a string and in this case, we want to remove all but one from that... clearly, if you wanted the last two characters you'd change the -1 to -2 etc etc etc.
After the length has been determined and the portion of that which is required - then the Right command will display the information you need.
This works well combined with an IF statement - I use this to find out if the last character of a string of text is a specific character and remove it if it is. See, the example below for stripping out commas from the end of a text string...
=IF(RIGHT(A2,(LEN(A2)-(LEN(A2)-1)))=",",LEFT(A2,(LEN(A2)-1)),A2)
Just another way to do this:
=MID(A1, LEN(A1), 1)

Resources