There are many times, particularly in accounting spreadsheets, where we are using codes e.g. Stock Codes, Nominal/Sales/Purchase Ledger Codes. Often there is useful information hidden within these codes, such as Bin Locations, Departments, Divisions, Regions, etc.

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.

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.

=LEFT("text",x) returns the leftmost x (first) characters in "text"

=RIGHT("text",x) returns the rightmost x (last) characters in "text"

=MID("text",y,x) returns x characters starting at position y in "text"

So, where the cell A1 contains the fixed format account code 123-456-789,we can pull out any of the three sets of numbers as follows:

To return the 123, use

To return the 456, use

To return the 789, use

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?

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","main text",x) returns the position of "text to find" in "main text" , starting at position x. This last argument is optional and it starts at 1 if left out.

=LEN("text") returns the length of "text", in characters.

so where A1 again contains the account code 123-456-789

Good Luck!

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?**

**Fixed formats**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.

=LEFT("text"

=RIGHT("text"

=MID("text"

To return the 123, use

**=LEFT(A1,3)**To return the 456, use

**=MID(A1,5,3)**andTo 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?

**Flexible codes****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"

=LEN("text"

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* argument for the LEFT function **=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.*

Good Luck!

**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"*.