Learn Excel - Stock Symbol (Last 2 Words) Duel 140 - YouTube

Channel: MrExcel.com

[0]
Bill: Hey. Welcome back. It’s time for another Dueling Excel podcast.
[3]
I'm Bill Jelen from MrExcel. I’ll be joined by Mike Girvin from ExcelIsFun.
[5]
This is our episode 140, stock symbol and price or extract the last two words.
[11]
Hey, Mike. I got a doozy for you.
[13]
This question came in from YouTube and she had actually seen one of our old dueling podcasts
[17]
where we split words and numbers out, but there was an X in there.
[20]
She said, well, wait a second.
[21]
What if you don't have an X, and this is a really tough one because, as I look at this,
[26]
we're trying to get the price which is the number at the end, and then the symbol which
[29]
is the word before that, and then everything else goes back into FUND NAME, and even FLASH
[37]
FILL has trouble with this.
[39]
So, if I type the first one and then start to type the second one, just I, see, they're
[45]
getting it wrong.
[46]
They’re including too much a lot of times.
[49]
So, I'm going to not accept that, and edit, and now that we have the two, let's see if
[56]
that's enough for FLASH FILL to figure it out.
[58]
ISHARES RUSSELL 2000, missed the 2000, fix that one.
[62]
There we go.
[63]
So, finally after typing the two and then correcting the one, it looks like FLASH FILL
[67]
is getting the FUND NAME correct, but let's see, with the symbol, that's working great.
[75]
The numbers though it, I haven't figured it out anyway.
[77]
I've tried a whole bunch of different things.
[80]
765, 764.58.
[82]
I've tried it with the ,. I've tried it without the ,. I know the FLASH FILL does not like
[88]
to use numbers.
[92]
Like, you have to ask them to go try and do FLASH FILL, and see, there, they’re missing
[96]
the 2.
[97]
So, if I fill that one in, yeah, so it's like [unintelligible – 1:40] is thinking the
[102]
, there is important and then it goes haywire after that.
[107]
Let’s see if we give it an example of 3 and try that, if they can figure it out.
[114]
Yeah.
[115]
Again, okay, so, it's really a tough data set, but she says she does not have Excel
[121]
2013.
[123]
She's all the way back on Excel 2003.
[125]
So, at this point, of course, I'm going to switch over to VBA.
[130]
With VBA, it's going to be fairly simple to attack this.
[132]
I'm going to figure out the length of the cell, and then I'm going to start from the
[135]
right edge and go backwards.
[137]
I’m going to ignore anything until I get back to the space.
[142]
Once I see the space, I know that, at that point, I have the price, and then everything
[146]
to the left of it is the description and the symbol, and then, again, I'm going to start
[150]
going backwards character by character until I find the space and I know that that's where
[155]
the data will go.
[156]
Now, this macro that I wrote is based on the selection.
[159]
I'm assuming that the 3 columns to the right are blank.
[161]
So, we’ll do ALT+F11 and here we go.
[166]
BREAKTHEMOUT.
[167]
FOR EACH CELL IN SELECTION, grab the cell value, and I actually…the first time I wrote
[171]
this, it would be much faster, now that I start to think about it.
[174]
I'm going from the end of the value TO 1 STEP -1, take a look at the character using the
[180]
MID function, and if it's 1 through 0, a , or a ., I ignore it.
[185]
I don't do anything.
[187]
Otherwise, I assume I've gotten to that space or the non-breaking space or whatever they
[191]
put in there and I'm good to go figuring out the price.
[195]
So, we start at the MID of MYVAL , i + 1.
[198]
I don't specify a length and, in VBA, that's nice.
[201]
You don't have to specify a length.
[202]
It just goes out to the end, and we get the price.
[205]
So, from the cell, we go out 3 columns, put the price in, and then change my value to
[211]
be the leftmost characters right up to the [unintelligible – 03:35] that space.
[217]
EXIT THE FOR.
[218]
We come down here to this next for.
[220]
Then, almost the exact same thing, we look for the character this time.
[223]
I'm just looking for the space, and when I find it, I know that I have the symbol, put
[228]
that 2 cells to the right, and then everything to the left of that is assumed to be the stock
[236]
name or description.
[237]
So, here, let's just try it.
[239]
We’ll press ALT+F8 and say break them out, click RUN, and perfect.
[245]
It gets everything without the hassle.
[248]
So, here, you know, FLASH FILL is really kind of fooled by this because it was a hard data
[255]
set, but because the VBA was able to go from the end and go backwards, working through
[261]
it, basically just pulling out the first or the last word, the next to the last word,
[266]
and then everything else, it seemed to be easier.
[269]
Mike, let's see if you have a formula that would do this.
[273]
Mike: Thanks, MrExcel.
[275]
Oh man, I don't like formulas like this.
[278]
This is just flat-out a hard formula to extract these things because there's no consistent
[282]
pattern.
[283]
Now, we'll come back to a formula in just a moment but, you know, FLASH FILL is absolutely
[289]
amazing if you have 2013 or later.
[291]
The trick is, though, you have to know your data.
[294]
If you don't know your whole column data and all the patterns and intricacies of the data,
[298]
then FLASH FILL can get into trouble, but as I look here, I see one word, two word,
[303]
three word, and second to last space.
[305]
I come down here, I see one word, two word, second to last space, one, two, three, four
[310]
before we get to the second to the last space.
[312]
So, if we give it those three examples, we know for sure, you know, and I did, like,
[316]
a formula for a huge column, and then looked at the max of these to try and find a pattern.
[322]
Of course, by the time you do those formulas, maybe it's just as easy to do a formula, but
[328]
let's try this.
[329]
This is good to know about FLASH FILL that…SPDR.
[333]
So, we have one example with three bits of text separated by a space.
[339]
I’m going to come down here.
[341]
POWERSHARES.
[343]
Oops, I better spell it right, and then QQQ, and then finally this one.
[353]
Now, it tries to FLASH FILL on these.
[355]
I'm going to come to the end and hit ENTER, and then, instead of coming up here, and you're
[360]
using this a lot, use CONTROL+E for FLASH FILL.
[363]
So, we gave it the 3 examples and it got everything correct.
[367]
TICKER, that one's easy.
[369]
SPY, CONTROL+ENTER, CONTROL+E.
[371]
Now, the numbers here, in my experience, the numbers get into trouble when you have 0s,
[377]
like dates, 07 for the month or times or pennies like this, because really we want to say,
[385]
go from the last space and extract all the characters.
[387]
So, I'm going to come down here.
[389]
I'm going to try and give it two examples here.
[393]
4 , 764.58 and then I'm going to come all the way down to the bottom or search until
[400]
you find one with the 01, and I'm going to say 484.01, CONTROL+ENTER, CONTROL+E, and
[407]
so that will get it.
[409]
0 is causing trouble with numbers sometimes.
[412]
Alright.
[413]
Now, a formula.
[414]
I just don't know any good fast way to do this.
[417]
The only way I know how to deal with this with variable number spaces before you want
[421]
to get the last one is we have to insert a character here.
[424]
So, watch this.
[425]
First, we're going to have to count how many spaces there are.
[428]
Say, the LEN of all those and then the LEN of, and we're going to use this substitute.
[434]
There's no SUN but there is a substitute function.
[437]
The text, , the old text is “ space ”. I'm looking for the spaces and now I want to put
[443]
new text “” that says put nothing.
[447]
So, then, I'm counting that.
[450]
So, it'll count the thing with all the spaces and then subtract the ones without them, and,
[455]
there, it tells us what the position of the last space is.
[459]
Not the position.
[460]
That means that's how many spaces there are.
[463]
Now, I'm going to have to insert a character there.
[468]
So, I'm going to have to do SUBSTITUTE again.
[470]
No SUN.
[471]
The text is going to be this , the old text I’m looking for is a space, and “. I'm
[477]
going to have to pick some text that's not in the text string ever, so I'm going to put
[482]
that, and that's the instance.
[484]
So, now, I've created a text string with a little character there.
[490]
Now, I have to search for that to figure out the position.
[493]
So, I'm going to search for ^ within that right there.
[499]
Oh, this is just ridiculous.
[501]
Alright.
[502]
So, now, I know the position of that, and I can use the replace, and I'm going to say
[509]
REPLACE.
[511]
The old text is this , the start number.
[514]
REPLACE goes from a certain number character to another number character and replaces something.
[519]
So, I’m going to say start at 1, go all the way to that number of characters, and
[523]
the new text I want is “.Now, there's a few different ways we do that but I tend to
[528]
use REPLACE.
[529]
[=REPLACE(A2,1,SEARCH(“^”,SUBSTITUTE(A2,“ ”,“^”,LEN(A2)-LEN(SUBSTITUTE(A2,“
[530]
”,“”)))),“”)]
[531]
That's just yucky.
[532]
Oh man.
[533]
It's text.
[534]
You can convert it to a number with 0.
[536]
I'm going to come over here.
[537]
I'm going to use that right there and use it to get the SPY now.
[541]
So, I'm going to say =SUB, not SUN.
[544]
I'm going to say, here's the text.
[546]
The old text is that right there.
[548]
Now, I want to get that space there as the lead.
[552]
So, I'm going to do space in “, &. That's the old text I want to substitute and now
[559]
I'm going to put nothing in there, and so that will give me, if I look at this SPY,
[566]
and now I can simply take the right, the right of that.
[575]
Whoops.
[576]
Right , 3.
[577]
[=RIGHT(SUBSTITUTE(A2,“ ”&D2,“”),3)]
[579]
Oh, man, this is making my head hurt, all this, and now I want to get the FUND and I'm
[585]
going to base it off of these.
[587]
So, I'm going to say =SUBSTITUTE again.
[590]
All these substitutes.
[591]
I'm going to take this, , and the old text I'm going to look for is that I want that
[597]
space before the SPY.
[599]
So, I'm going to put a space &, join it to SPY, and then a space between this and that.
[607]
That little string right there will search for that and, of course, for when we find
[611]
that, the new text is going to be nothing, CONTROL+ENTER.
[613]
[=SUBSTITUTE(A2,“ ”&C2&“ ”&D2,“”)]
[615]
Oh man.
[616]
That is an absolute mess, and I'm sure there's a better way to do it, and I've done a number
[622]
of formulas like this over the years and they always make my head hurt, but that's the only
[626]
way I know is insert a little thing there and then there's these.
[631]
Alright.
[632]
FLASH FILL.
[633]
I wish FLASH FILL would be…you know, FLASH FILL is awesome.
[637]
It's only as good as our knowledge of the data set and our choice of examples.
[644]
Alright.
[645]
Throw it back to MrExcel.
[646]
Bill: Hey.
[647]
Alight.
[648]
Mike, that was really, really cool.
[649]
great tip there with FLASH FILL.
[650]
I've never seen filling in 3 examples before.
[652]
You do the FLASH FILL and then substitute one of those functions that I’D never think
[659]
to use.
[661]
Great use there.
[662]
It'd be great if we had a function that was called reverse that would take a text string
[666]
and reverse it.
[667]
Then, it would be easy to find the first space, second space, and it would be very predictable
[672]
then to get the answer and reverse those again, but, unfortunately, we still do not have a
[678]
reverse function.
[679]
Alright.
[680]
Well, hey.
[681]
I want to thank everyone for stopping by.
[682]
We’ll see you next week for another Dueling Excel podcast from MrExcel and ExcelIsFun.