Extract number between brackets in Excel? - excel

I've been searching up and down for an answer to this, but haven't found anything I can get to work. It may well be my lack of knowledge in the more "codey" aspects of Excel.
Anyway, this is my situation:
In C2-C17, I have some text set up like this:[2] Name of job.
The "[2]" indicates the hours we expect the assignment to take, the rest is the name of the assignment.
Now I need to calculate how many hours all this adds up to, so in B2-B17 (the numbers will change) I would like to extract the number between the brackets, in this case "2", so I can simply calculate and add them up.
How would I go about doing this? I've spent far too long at work by now, trying to find a solution that works for me.
Any help would be very much appreciated!

Use the formulas: MID(cell,start_num, length) and FIND(text,cell)
So, the formula is:
=MID(B3, FIND("[",B3)+1, FIND("]",B3) - FIND("[",B3) - 1)
Which means:
Extract the text from the cell B3, starting from the location of character "[" plus 1 in the same cell, and a total of chars calculates by: location of "]" - location of "[" minus one.

Copy to a new column, Find & Replace, Find what:
*[
Replace All. Find what:
]*
Replace All.

There is a function to work with substrings: "MID"
The C3 cell have formula. We assume that string will always start from [ and then will be number of hours (1 digit).
Than I just expand to other cells froc C3 to F3
You can add substitute function to work with two digit numbers.

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.

Excel: referencing a portion of file name only

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

How to get rid off only certain zeros formula?

Ok so I had a nice formula until a problem came along. Basically I needed to get rid off a zeros in the middle of a 10 characters String/Range i.e AB00005879 to do that I have used formula SUBSTITUTE(NameRange,"0","") which gave me nice AB5879 solution. Sometimes the number at the end would only be 3 digit long AB00000975 so my formula would give me AB975 All great until I stumble a problem. Some of the strings came in a form of i.e. AB00004020 So my formula extracted every zero leaving me with AB42. Is there a way to extract only first four zeros in a middle an always keep the number at the and? so the last scenario would look like AB4020. Thanks in advance
SUBSTITUTE(NameRange,"0",""))
If you always have two characters at the start and then some zeros and then some numbers, all of which you want to keep, this should work
=LEFT(A1,2) & VALUE(RIGHT(A1,LEN(A1)-2))
EDIT #2
If your string always starts with two letters such as AB following by a random number of zeros and then a number string that you want to keep, try
=LEFT(A1,2)&RIGHT(A1,11-AGGREGATE(15,6,ROW($3:$10)/(--MID(A1,ROW($3:$10),1)>0),1))
Replace A1 with your actual case.

Excel: Extracting a full number from a cell - bringing it all together :)

First off, thank you to all how have helped get me to this point. I'm so close! On to the scenario, which I apologize in advance is a bit of a work in progress.
I have text in a cell and I need to extract a number. The tricky part is there are various situations to address.
The number may immediately follow a "#" and could vary in length. People on Stack Overflow helped me with coming up with this which works great:
MID(B2,(FIND("#",B2,1)+1),FIND(" ",B2,FIND("#",B2,1)+1)-FIND("#",B2,1))
That was a huge leap forward, but there are also situations where there is no # sign and the cell might have "abc (1205) 645 chan", where I need to extract the 645.
I'm using this, below, in conjunction with an on error statement for when there is no "#"
TRIM(MID(B53,(FIND(" " &{"1","2","3","4","5","6","7","8","9"},B53,1)),FIND(" ",B53,FIND({"1","2","3","4","5","6","7","8","9"},B53,1))-FIND({"1","2","3","4","5","6","7","8","9"},B53)))
So I use the first Mid/Find to avoid the (1205) and find the next " x" where x is a number. The problem is it seems I have trouble when the number I'm searching for has 1 or 3+ numbers in it, but if it has 2 I return the value just fine.
It seems I'm very close but just not there yet.
This formula will return the number that follows either a # or a ) in your string. If that pattern does not exist, it will return a #NUM!` error
=AGGREGATE(14,6,--MID(A1,MIN(FIND({"#",")"},A1&"#)"))+1,{1,2,3,4,5}),1)
Note the array constant as the num_chars argument of the MID function. The maximum number should be at least the largest number of digits (or decimal + digits) plus any spaces between the delimiter and the first digit, that might be expected to be found.
EDIT: If your version of Excel is prior to 2010, and does not have the AGGREGATE function, you may use this array-entered formula instead, so long as the values to be returned will be positive numbers:
=MAX(IFERROR(--MID(A1,MIN(FIND({"#",")"},A1&"#)"))+1,{1,2,3,4,5}),0))
This formula must be entered by holding down ctrl+shift while hitting enter

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.

Resources