Extracting certain numbers from a cell containing numbers and special characters - excel

I have cells that contain both numbers and special characters such as this:
[1:250:10]
The 'coordinates' shown above can be in the following format.
[(1-9):(1-499):(1-15)] in terms of what numbers can be within each part.
How do I extract these three numbers into three separate cells?

Assuming your data is in Cell A1 the to extract first number use following formula
=MID(A1,2,(FIND(":",A1,1)-2))
for second number use
=SUBSTITUTE(MID(SUBSTITUTE(":" & A1&REPT(" ",6),":",REPT(":",255)),2*255,255),":","")
finally for third number enter
=SUBSTITUTE(TRIM(RIGHT(SUBSTITUTE(A1,":",REPT(" ",LEN(A1))),LEN(A1))),"]","")

Just tossing out some other options.
First number since it only has a length of 1 digit and is on the left side, use the following:
=RIGHT(LEFT(A1,2))
second number will be found by locating the : in the string
=MID(A1,FIND(":",A1)+1,FIND(":",A1,FIND(":",A1)+1)-(FIND(":",A1)+1))
third number will be dealt with in the same way as the second but we will use the second : and the ] as the identifiers as to where to grab from and how much to pull.
=MID(A1,FIND(":",A1,FIND(":",A1)+1)+1,FIND("]",A1)-(FIND(":",A1,FIND(":",A1)+1)+1))
now all those number will actually come through as text. If you want to have them as numbers in the cells, send them through a math operation that will not change their value. Do something like +0, -0, or *1 at the end. Alternatively you could add -- at the start of each formula (yes that is double - incase you were wondering if it was a typo)

Related

How to select a few numbers from a given number

If I have entered a certain 4 digit number for example ,1234 how do I choose like the first 2 numbers or the last two numbers from the cell by that i mean suppose i want it to return 34 for the last two digits and I want it to return 12 for the first two digits. So anytime I change my 4 digit number it works the same way.
You may use the LEFT and RIGHT functions, e.g.
=LEFT("1234", 2)
=RIGHT("1234", 2)
If your 4-digit number is, in fact, a string you can parse the string as suggested by #Tim Biegeleisen. In my Excel 365, when I enter 1234 in a cell formatted as General I can use the same method.
=LEFT(A1, 2)
and
=Right(A1, 2)
However, this conversion of a number to text mustn't be presumed to work under all circumstances. Therefore you may prefer to convert the number to text explicitly in the formula.
=LEFT(TEXT(A1,"0000"), 2)
and
=Right(TEXT(A1,"0000"), 2)
This method has the added advantage of being able to handle numbers of less than 4 digits.
On the other hand, you can also extract first and last digits from a true number, without converting it to text.
=INT(A1/100)
and
=MOD(A1,100)
The main difference is that the results are also numbers (all partial strings are text). Therefore this would be the preferred method if you don't want to worry about strings, text, numbers, numerics and cell formats.

Concatenate Custom Function

On a daily basis I need to load data to one of our systems. However Excel deletes the previous zeros in front of the contractor IDs. So i have to add THREE zeros manually. I normally use the CONCATENATE function however now the IDs are coming differently so some IDs now only need to have TWO zeros added.
example:
ID
911111
I use concatenate to make it look like:
000911111
I came up with the IF formula that detects if the ID starts with a number NINE, to concatenate TWO zeros and if not, then to add THREE zeros.
example:
=IF(LEFT(A32,1)="9",CONCATENATE("00",A32),CONCATENATE("000",A32))
Now I want to create this formula as a custom defined so I do not have to write down the formula ever time I work on the data every day.
Any suggestions I will really appreciate.
In addition to the formatting responses provided in the comments, you could use the RIGHT function to cut off the leading zeroes to the appropriate amount.
For example, assuming A1 holds a string of numbers, between 0 & 9 digits long. We can create text representing a 9 digit string, with as many leading zeroes as necessary, as follows:
=RIGHT(REPT("0",9) & A1,9)
REPT("0",9) tells Excel to repeat the character "0" 9 times. It then tacks on whatever text is in A1. Then it takes only the rightmost 9 characters of the concatenation.
I generally would recommend the Formatting options noted in those comments, unless you need the text to be 9 characters for other formula purposes.

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.

I'm trying to make a formula that checks a number in excel, but the cell has to include the dashes

I have something like this:
A1: ABC-DE442
B1: 0069-1234-12
I'm trying to make a formula that will look at A1, get the number 442, and then check if B1 is in the format of 4 numbers, a dash, 4 numbers, a dash, and then finally 2 numbers. I want to make sure that this could work with any number besides 442 though. So if I had like 123 instead of 442, it would need to be 1 number, dash, 2 numbers, dash, and three numbers.
It would also need to give me an error if I had something like this:
A1: ABC-DE442
B1: 004-2345-34
because it only has three numbers in the first section before the dash.
Hopefully I have been clear enough in what I'm trying to do. Any help would be greatly appreciated.
So in the end, this is the giant formula I had to create:
=IF(LEN(LEFT(J4,FIND("-",J4)-1))=VALUE(MID(I4,9,1)),IF(LEN(LEFT(MID(J4,FIND("-",J4)+1,999),FIND("-",MID(J4,FIND("-",J4)+1,999))-1))=VALUE(MID(I4,10,1)),IF(LEN(MID(MID(J4,FIND("-",J4)+1,999),FIND("-",MID(J4,FIND("-",J4)+1,999))+1,999))=VALUE(MID(I4,11,1)),"True","Doesn't match code part 3"),"Doesn't match code part 2"),"Doesn't match code part 1")
J15: 0069=LEFT(J5,FIND("-",J5)-1)
J16: 4=VALUE(MID(I5,7,1))
J17: 4=LEN(J15)
J18: 0469-56=MID(J5,FIND("-",J5)+1,999)
J19: 0469=LEFT(J18,FIND("-",J18)-1)
J20: 4=VALUE(MID(I5,8,1))
J21: 4=LEN(J19)
J22: 56=MID(J18,FIND("-",J18)+1,999)
J23: 2=VALUE(MID(I5,9,1))
J24: 2=LEN(J22)
Hopefully all of these formulas will make it easier to understand how I combined everything together.
I essentially put every formula into one giant one using the information used in the answer I said was right. For the first part, I took the LEN(D1) but had it all written out, and then set it equal to the =VALUE(MID(A1,7,1)).
Then, for the true part of that if statement, I had to first get A1 without D1, then get it to just be the middle section using the LEFT formula, and then finally taking the LEN of that. Checked to see if it was equal to the =VALUE(MID(I4,10,1)), which was the next number in B1.
If that was true, I went to the final IF statement which again had to go through the same process of getting the last section of A1 on it's own, taking the length of that, and seeing if it was equal to =VALUE(MID(I4,11,1)).
Finally, I just created different print statements for the last true, and every false section of the IF statements.
Moral of the story, I was able to make a formula that works with every single number combination because it turns out B1 could only be in the form of AB - CDE###, so I could always find that number in B1 and compare it.
This should be possible with Excel formulae but it will get a bit complicated. The functions you're probably going to want to make use of are FIND, LEFT, LEN, MID, and VALUE.
For example,
C1: =FIND("-",B1)
will return the position of the first dash in the string to be tested, which for your example is 5. Then
D1: =LEFT(B1,C1-1)
would return the characters before that dash, i.e. '0069'. Meanwhile you need to get the lengths of the three runs of digits out of your first string (the one in A1). Will that string always be the same length? If so you can use e.g.
=VALUE(MID(A1,7,1))
which returns the 7th character of the string in A1 and converts it to a number (4). Now you can test whether that value is equal to the length of the first run of characters, i.e. LEN(D1).
But are those characters all numeric? Well, you can try converting them to a number with VALUE(D1) which will return #VALUE if the argument can't be made into a number.
OK, that checked the first run of characters, what about the rest of them? Well,
=MID(B1,FIND("-",B1)+1,999)
will give you the remainder of the original string after the first dash, which you can analyse in the same way, and so on.
It's easiest to develop these formulae by using lots of cells to hold the intermediate values as you work them out, but once you're confident these are working OK you can consolidate them into fewer cells. Whatever you come up with, make sure you test it with lots of different inputs to check that your formulae respond to them in the way that you expect. (In particular, check what happens if you have one or more spaces after a dash, as VALUE will ignore leading spaces - you might want to use a SUBSTITUTE function on your initial text to remove any spaces.)

Excel Number Formatting

I'm currently working on a sheet that contains part numbers in it. I'd like them to be formatted like this:
####-#####-XX
Where #s can be letters or numbers, #s are numbers, and Xs are letters.
I run into two problems while doing this. The first is that I can't figure out how to handle text and numbers at the same time in the Custom Format dialog box. The second is that occasionally a part number will have 3 letters after the second hyphen rather than 2, and I can't figure out how I should structure the condition to differentiate between the two formats.
How can I handle numbers and text at the same time when creating a custom format, and how can I add the condition described above (based on character numbers or something)?
Thanks.
If can't be achieved with custom formatting then a formula such as below may suit:
=LEFT(A1,4)&"-"&MID(A1,5,5)&"-"&RIGHT(A1,LEN(A1)-9)
If the middle number section has to be 5 digits, use
#-0000#-XX
But I don't think number formats are designed to handle Alphanumeric entries, and I can't help you with those X's

Resources