transcript

excelisfun - Excel Magic Trick 1093: Extract Date from Middle of Description: Text To Columns or Formula?

Thumbnail of the video “excelisfun - Excel Magic Trick 1093: Extract Date from Middle of Description: Text To Columns or Formula?”
Download Markdown
Community upload. AI output has not been independently verified.

Welcome to Excel Magic Trick number 1093. If you want to download this workbook and follow along, click on a link below the video. In this video we have some descriptions, and we need to get the date out of the description into a column. We could do this with a formula, and every time I try to do data extraction from the middle of a text string, I have like 10 different ways I can do it, and it's never the most efficient. But no problem: if we don't need a formula, if we don't need to make it automatic, we can use Text to Columns.

We'll come back to the formula in just a moment, but Text to Columns is absolutely easy. Either way, whether we're using a formula or Text to Columns, we still have to recognize a pattern, and it looks like the date is between dashes. Let's highlight this column and use that dash as a delimiter, meaning a separator between different bits of data, and use Data, Text to Columns. Or we can use the keyboard, in Excel 2007 or later: Alt, A, E.

All right, so there's our delimiter, some character. Click Next. We're going to slow down here. It usually comes up like this, and right there is no delimiter, so it's all one thing. We uncheck this, we say Other, and I'm going to type a dash. We can see the preview. There are some spaces here, but we'll deal with that in the next step. The cool thing about this step is we can choose whichever column we want and say skip it if we don't want it. Skip it, we don't want it. Skip it. We want this one, because there's that space there. I'm going to say import it as a date, Month Year, Date. Now, I don't want to replace this column of descriptions with the date, so I'm going to very carefully say the destination is here, right there. When I click Finish, just like that, we have our dates. That is a lot easier than a formula solution.

I'm going to use SUBSTITUTE, and the problem is there are two different dashes, so if we use SEARCH to search for the position of that dash, it can find the first one. So I need to find the second one, and absolutely, we can use SUBSTITUTE. SUBSTITUTE has an option to specify which instance, so when we get to that, we'll say number two. So the text is this cell, the old text I'm looking for is a dash in double quotes, and the new text I want to insert is something different than any of our descriptions, so I'm going to use the caret, or circumflex, and then a dash. We've recognized the pattern: there's always going to be two dashes. Now we want to put a marker there and substitute it in. Press Ctrl+Enter, and now we have inserted, or substituted, a character.

Now I'm going to find the position of that by using SEARCH. I'm going to say search for a caret within that text. Control-Enter, double-click, and send it down. That will tell me 32, but I actually want to back out, so that would be 31, and then 30, so I'm going to subtract 2. That will tell me the position in this text string of the last character in the date. Now I'm going to use LEFT and say go from the left and only give me that many characters, which will remove everything at the end. So LEFT of that, how many characters, there. Let's try Ctrl+Enter. I had it highlighted, so it was in edit mode; I edited the formula in the active cell, and Control-Enter populates that down. So now I've got the date at the end.

Now I'm going to use REPLACE. In essence, I'm saying: replace, going from number 1 to where the dash is, with nothing. All of that will be nothing, and what will be left is the date. So I'm going to say REPLACE, here's the old text, the starting position is 1, and the number of characters: I can use SEARCH for a dash in double quotes within the original text over here, and close the parentheses. That will find the position right there, and I wanted to go one more, so I'm going to add one. Now we have starting position one and number of characters, however many that is, F9 shows that's twenty-two. So we can simply say, in our final argument, what's the new text: double quotes, double quotes. That removes everything. Close the parentheses, Control-Enter. There's our date, but it's text. Any of these functions, FIND, REPLACE, LEFT, RIGHT, SEARCH, and all those kinds of things, spit out text. So we're simply going to do a math operation, like plus zero, and that converts it back to a date. Oh, and it's a serial number, which is pretty cool. It took that text and made it a serial number. Control+1 to format cells as a date, click OK, and now we have our dates.

There are other possibilities for formulas here. If you've got a good idea, let's see: I used five functions here. Go ahead and post your version in the comments. We'll see you in the next extract.