Best Microsoft Excel Bloggers
Showing posts with label Text Functions. Show all posts
Showing posts with label Text Functions. Show all posts

Thursday, November 17, 2011

LEN


I was getting my U.S. Christmas mailing list in order and noticed a problem on some of my zipcodes. Many of New England's zip codes begin with a zero so when I pulled my addresses into Excel to sort and filter them, Excel dropped the 0 from the beginning of the zipcode so for example, instead of 02186, Excel displayed 2186.
(It did this because it assumed the ZipCode was a number instead of text.)





The fix is easy.

In this case, I clicked in cell I2  and entered =IF(LEN(H2)=5,H2,"0"&H2)




LEN() counts the number of characters. So, I told Excel -if the number of characters in cell H2 total 5 then just display the contents of cell H2. However, if the number of characters in H2 do not total 5 then include a 0 at the beginning of the H2 cell contents. I used an ampersand (&) to tell Excel to join the 0 and the zipcode together  however you could also have used the Concatenate() function- I just think the & is faster.  Both LEN() and Concatenate() are Text functions.

Once I copied the formula down, I then selected all the zipcodes in Column I, copied them and then did Paste Values so that the formulas were overwritten with the actual numbers. I did that so that when I deleted Column H, my new zipcodes would display.


An alternative way would be to just hide Column H. 




 
This  IF assumed residential US zipcodes. If you had business zip codes which would have more than 5 characters or were including European postal codes then you would have to change the IF a bit.


Wednesday, December 1, 2010

Extracting Text of Different Lengths

Use the Search function to separate text


Most people are familiar with LEFT, RIGHT and MID as ways to separate text and they are useful -
but only when all the cells have the same number of characters.
If you want to separate employees' names or emails you can't use LEFT, RIGHT and MID by themselves because all the names and emails are different lengths. This is where the SEARCH command comes in.

In the example below, I want to separate my first name from my last name so to do that I need to combine the LEFT function and the Search function.
The actual function is =LEFT(A2,Search(" ",A2)-1.
Excel searches A2 for a space as denoted by the empty quotation marks, in the formula, and finds it between my first and last name so at this point the formula would read as =Left(A2, 9-1)









So, what does that mean? Excel searched and it found the empty space. This empty space is the 9th character. We subtract 1 to tell Excel we only want 8 characters. The LEFT function then retrieves the 8 characters to the left in cell A2 and returns my name Patricia.

That was the easy one. Retrieving the last name is a little bit more work. In this case, I am using MID and SEARCH.In case you are not familiar with MID, the syntax is =MID(cell reference, start_number, number of characters)


The formula is =MID(A2,SEARCH(" ",A2)+1,35).
Excel reads that as =MID(A2,10,35)

To break the formula down, I again told it to search for the empty space between the first and last name and to use that as the starting point. Then, I told it to go over 1 character and return the next 35 characters. I arbitrarily picked 35 since few last names would have more than 35 characters.

Below are examples, if you wanted to pull apart an email address.





We finally have enough fans to have our own Facebook URL. If you have a chance, check it out and click on the LIKE button while you are there.  If you Facebook, all of these blog entries also appear there as do my tweets.  It is like one -stop shopping!

I am watching tiny snow flakes coming down as I type this. Have a good week and stay warm.

Thursday, November 18, 2010

Converting Text with a trailing CR to a Number

,Don't you just hate it when you import data and see the dreaded CR at the end of a number. You know that Excel automatically treats any cell content as text if it is a mix of numbers and text. This of course is a huge problem if you need to actually use that cell value in a calculation.  Below is a way to convert the content by using the LEN function and the LEFT function.


  • The LEN sounds like a useless function but it actually very powerful - it counts the number of characters in a cell =LEN(text). 
  • LEFT is a text function and it extracts the specified number of characters =LEFT(text, number of characters).

The LEN function counts the number of characters in the cell and the LEFT function tells Excel to extract everything except for the last 2 characters in that cell. I then multiplied it by -1 to make it negative.


So, in the example above, if you break it down, Excel looks at the LEN (A20)-2 and evaluates that as 5-2 which of course is 3 so now it would read =LEFT(A20,3)*-1. Excel would treat that as =100*-1 and the resulting answer would be -100.




Ms. Excel- Resident Excel Geek