How to Extract Text Before Second or Third Instance of a Symbol

For example, I have the following list in cell A1:

“Coffee, cream, sugar, tea”

I am trying to copy all the text before the second or third instance of the comma into an adjacent cell.

To extract the text before the second instance of the comma, use the ff. formula:

=LEFT(A1,FIND("^^",SUBSTITUTE(A1,",","^^",2))-1)

where the 2 represents the comma instance number

The result: Coffee, cream

Posted in Extracting words | Leave a comment

Alternative to VLOOKUP: INDEX & MATCH

VLOOKUP and HLOOKUP take up a lot of memory and processing power, especially if you have a large data set. A great alternative to VLOOKUPs and HLOOKUPs are the INDEX & MATCH functions. Below is an example from Mr. Excel.

The Problem

Retrieve Data

One day, you have a situation where you have the employee name, but need the employee number. In the image above, you have a name in A10 and need to find the employee number in B10.

When the key field is to the right of the data you want to retrieve, VLOOKUP will not work. If only the VLOOKUP would accept -1 as the column number, there would be no problem. But, it doesn’t. One common solution is to temporarily insert a new column A, copy the column of names to the new column A, populate with VLOOKUP, Paste Special Values, then delete the temporary column A. Excel pros can probably do this move in their sleep.

I am going to suggest you take the challenge and try to use this single step method. Yes, you will have to tack the formula up on your wall for a few weeks, but you did that with VLOOKUP a long time ago, too, didn’t you?

I think the reason this is so difficult is that you are using two functions which you probably never used before. So, let me break it down into two pieces.

The Solution: INDEX & MATCH

INDEX Function

First, there is the INDEX() function. This is a horribly named function. When someone says “index”, it does not conjure up anything in my mind that is similar to what this function does. Index requires three arguments.

=INDEX(data range, row number, column number)

In English, Excel goes to the data range and returns you the value in the intersection of the (row number)th row and the (column number)th column. Hey, think about it – this is pretty simple, right? =INDEX($A$2:$C$6,4,2) will give you the value in B5.

Applying INDEX() to our problem, you can figure that to return the employee number from the range, you would use this: =INDEX($A$2:$A$6,?,1). Actually, this piece of it seems so trivial that it seems useless. But, when you replace the question mark with a MATCH() function, you have the solution.

MATCH Function

Here is the syntax:

=MATCH(Value, Single-column data range, FALSE)

It tells Excel, “Search the data range and tell me the relative row number where you find a match for (data). So, to find which row has the employee in A10, you would use =MATCH(A10,$B$2:$B$6,FALSE). Yes, this is more complex than Index, but it should be right up the alley of VLOOKUP pros. If A10 contains “Miller, Bob” then this MATCH will return that he is in the 3rd row of the range B2:B6.

INDEX and MATCH Functions Together

There it is – the MATCH() function tells the Index function which row to look in – you are done. Take the Index function, replace our question mark with the MATCH function, and you can now do the equivalent of VLOOKUPs when the key field is not in the left column. Here is the function to use:

=INDEX($A$2:$A$6,MATCH(A10,$B$2:$B$6,FALSE),1)

The sticky note on my wall actually shows it as two lines. First I wrote out the explanation for MATCH(). Below that I wrote the explanation for INDEX(). I then drew a funnel shape between the two to indicate that the MATCH() function drops in to the 2nd argument of the INDEX() function.

The first few times I had to do one of these, I was tempted just to slam a new temporary column A in there, but went through the pain of doing it this way instead. It is faster, and requires less manipulation. So, the next time you are wishing you could put a negative number in the VLOOKUP function, try this strange combination of INDEX and MATCH to solve your problems.

 

Posted in Functions | 38 Responses

How to add or insert a line break or carriage return in a cell in Excel

Want to add insert line breaks and carriage returns from a cell in your Excel file?

Let’s say, we have the following example:

John Smith

But now we need these two words to appear on two separate lines instead of a single line, just like this:

John
Smith

There are two options to get the job done.

Option 1

Launch a Microsoft Word or a Notepad file. Enter the text (including the line break) that you’d like to insert into your Excel cell. Copy and paste those text into the cell.

Option 2

Use the shortcut Alt+Enter to put the last name on a new line.

Posted in Functions | Leave a comment

How to remove line breaks and carriage returns in cells in Excel

Want to remove line breaks and carriage returns from a cell in your Excel file without having to do it manually? The good news is, you can use the “find and replace” function on Excel to get this job done.

Let’s say, we have the following example:

Initial dataset

In this case, the author has used the shortcut Alt+Enter to put the last name on a new line.

But now we need it together on a single line, with a space between the first name and last name instead of a line break.

Instead of:
Bastien
Mensink

we need:
Bastien Mensink, etc. etc.

If you want to replace these line breaks with a space, you can do this with Excel’s “find and replace” command.

  1. Press CTRL-F to launch the “find and replace” window.
  2. In the “Find what:” field, enter the line break code, which is character number 10. To do this, press and hold the Alt key, and then enter the numbers “010” using the numeric keypad on the right-hand side of your keyboard (the numbers above the letters on your keyboard won’t work). You’ll then see a small dot blinking when your cursor is still in the field. If you have data imported from another source, sometimes the line breaks are also represented by character 13 instead of 10. If “010” does not work, try “013”.
  3. In the “Replace with:” dialog, you can enter a space to replace the line breaks with spaces. You can also leave that blank to completely remove the line breaks without replacing them with anything.
  4. Press “Replace All”.

Excel's Find and Replace dialogue

*Samples and screen shots were derived from ASAP Utilities.

Posted in Functions | 6 Responses

How to remove all hyperlinks in an Excel file

Looking to remove all the hyperlinks in an Excel file?

Hit [ALT]+[F11] to open the Visual Basic Editor

Go to “Insert” > “Module” and in the pop-up window copy:

Sub RemoveHyperlinks()
'Remove all hyperlinks from the active sheet
ActiveSheet.Hyperlinks.Delete
End Sub

Then click “File” > Close and return to Microsoft Excel

You can now run the Macro in Excel by going to:

Tools > Macro > Macro and then Run “RemoveAllHyperlinks”, this will delete all URLs on the selected worksheet.

Posted in Macros | 2 Responses

How to remove line breaks from Excel

Want to remove a line break in Excel? Follow the steps below!

  1. In Excel, choose Edit>Replace
  2. Click the “Find what” box
  3. Hold the Alt key, and (on the number keypad), type “0010”. This represents a line break in Excel. You won’t see anything in the “Find What” box
  4. In the “Replace with” box, type the characters that you want to use instead of a line break
  5. Click the “Replace All” button.

Voila!

Posted in Functions, General tips | Leave a comment

How to remove the last character in a cell in Excel

For example, you have the following the text in a cell “A1” and you’d like to remove the last character “s”:

“I want to remove the last character of this cells”

To remove the last character “s” from A1, use the following formula:

=LEFT(A1,LEN(A1)-1)

The result:

I want to remove the last character of this cell

Not working for you?

Remember to replace A1 with the cell that contains the string of words you want to parse out.

Posted in Functions | 6 Responses

How to capitalize the first letter in a cell in Excel

If you’d like to capitalize all letters in a cell, it’s easy – just use the following formula (assuming cell “A1” has the text you want to capitalize).

=PROPER(A1)

But what if you just want to capitalize the first word in the cell “A1”? For example, you have the following  text in a cell “A1” and you’d like to capitalize just the first letter:

“How to Get a High School Diploma in 1 Day”

To capitalize “How” in cell “A1”, use one of the following formula:

=REPLACE(A1,1,1,UPPER(LEFT(A1,1)))

or

=CONCATENATE(UPPER(LEFT(A1,1)),RIGHT(A1,LEN(A1)-1))

The result:

“How to get a high school diploma in 1 day”

Not working for you?

Remember to replace “A1” with the cell that contains the string of words you want to parse out.

Posted in Functions | 46 Responses

How to delete or remove the first two words in a cell

For example, you have the following  text in a cell “A1” and you’d like to remove the first two words:

“English courses USA”

To remove the first two words “English courses” from A1, use the following formula:

=MID(A1,1+FIND("~",SUBSTITUTE(A1," ","~",2)),255)

The result:

USA

Not working for you?

Remember to replace A1 with the cell that contains the string of words you want to parse out.

Posted in Extracting words | 1 Response

How to extract words before and after a comma

Today, I encountered a situation where I had to separate the full names of our customers into the first and last names. The names are comma separated, so I was looking for an Excel formula that can help me extract the last name before the comma and the first name after the comma.

For example, you have the following text in a cell “A1″ and you’d like to extract the text before the comma (the last name):

“Doe, John”

To extract the last name before the comma “Doe” from A1, use one of the following formula:

=LEFT(A1,(FIND(",",A1,1)-1))

The result: Doe

To extract the first name after the comma from A1, use one of the following formula:

=MID(A1,FIND(",",A1)+2,256)

The result: John

Not working for you?

Remember to replace A1 with the cell that contains the string of words you want to parse out.

Posted in Extracting words | 34 Responses