By pulling out this information, we can then lookup additional information, sort or filter by it, or use it for analysis in a pivot table.
But how do we get at it?
The simplest situation is where the code is a fixed length and the information we need is always in the same position.
Here we can use the function LEFT, RIGHT or MID depending on its position.
To return the 123, use =LEFT(A1,3)
To return the 456, use =MID(A1,5,3) and
To return the 789, use =RIGHT(A1,3)
The functions above will allow you to extract any section of a fixed format code, but what if the code has a more complex structure?
Sometimes a code may have a less fixed structure, for example if in the code above, the length of the code was variable, as was the length of each of the three components - so it could take (for example) the form 12-345-67, or 1234-56-789. As long as there is some indicator of where the change is, then we can still do it. In this case we have the "-".
We can use the functions above, but we also need to use the functions FIND and LEN to calculate the arguments for those functions.
=FIND("text to find"
so where A1 again contains the account code 123-456-789
=LEFT(A1,FIND("-",A1)-1) returns 123
Explanation: FIND("-",A1) returns the position of the first "-" and by deducting 1 we have calculated the position of the last character of the first section (3) and therefore the x
=MID(A1,FIND("-",A1)+1,FIND("-",A1,FIND("-",A1)+1)-1-FIND("-",A1)) returns 456
Explanation: Not as scary as it looks when you break down the three arguments for the MID function.
The first argument is the easy one, just the cell with the code in, A1
The second argument, being the starting point of the code we are looking for is FIND("-",A1)+1, which is simply the position of the first "-", plus 1.
The third argument is the tricky one which needs to find the number of characters to return. We do this by finding the position of the character before the second "-" and deducting the position of the first "-"
FIND("-",A1,FIND("-",A1)+1) returns the position of the second "-" by using the FIND function again but using the optional start position argument so that we do not start looking until the character after the first "-". We have already worked out a formula to find this position (as it was the second argument of the MID function. We then deduct 1 to get the character before it.
FIND("-",A1) returns the position of the first "-" which we deduct, giving the full third argument of the MID function as FIND("-",A1,FIND("-",A1)+1)-1-FIND("-",A1).
=RIGHT(A1,LEN(A1)-FIND("-",A1,FIND("-",A1)+1)) returns 789
Explanation: Here we use the length of the string minus the position of the second "-" (calculated as above) to determine the number of characters to return from the RIGHT function.
There are many other possibilities, but a combination of these functions should give you a route to pull out pretty much anything from a code.
If you enjoyed this post, go to the top left corner of the blog, where you can subscribe for regular updates and get your free report "The 5 Excel features that you NEED to know".