Nájdite poslednú pomlčku - tipy pre Excel

Dnes je šialená otázka. Máte stĺpec s číslami dielov. V čísle dielu je kdekoľvek od 4 do 7 pomlčiek. Chcete extrahovať iba časť čísla dielu za prvou pomlčkou a až po poslednú pomlčku, ale okrem nej. Toto je súbojová epizóda Excelu.

Pozeraj video

  • Cieľom je nájsť prvú a poslednú pomlčku a udržať všetko medzi nimi
  • Najťažšou časťou je nájsť poslednú pomlčku
  • Bill metóda 1: Flash Fill
  • Ručné vyplnenie prvých pár (vrátane niektorých s rôznym počtom pomlčiek)
  • Vyberte prázdnu bunku pod tým
  • Ctrl + E do Flash Fill
  • Mike Metóda 2:
  • Použite Power Query
  • V Exceli 2016 je Power Query v skupine Získanie a transformácia v Exceli 2016
  • V programoch Excel 2010 a 2013 si stiahnite Power Query od spoločnosti Microsoft. Na páse s nástrojmi vytvorí novú kartu Power Query
  • Konvertujte svoje údaje do tabuľky pomocou kombinácie klávesov Ctrl + T
  • Použite rozdelenie údajov v Power Query - najskôr sa rozdelte pri pomlčke úplne vľavo, potom sa rozdeľte pri pomlčke úplne vpravo
  • Bill metóda 3:
  • Funkcia VBA, ktorá iteruje od konca bunky dozadu, aby našla poslednú pomlčku
  • Mike Metóda 4:
  • Pomocou SUBSTITUTE vyhľadajte umiestnenie N-tej pomlčky
  • SUBSTITUTE je jediná textová funkcia, ktorá umožňuje určiť číslo inštancie
  • Ak chcete zistiť, ktoré číslo inštancie, použite =LEN(A2)-LEN(SUBSTITUTE)

Prepis videa

Bill: Hej. Vitaj späť. Je čas na ďalší podcast Dueling Excel. Som Bill Jelen z MrExcel. (Pripojí sa ku mne Mike Girvin z ExcelIsFun. Toto je naša - 00:03) epizóda 185: výňatok z prvej - do poslednej -.

Dobre. Dnešnú otázku posiela Anvar na YouTube. Ako môžem extrahovať všetko od prvého - po posledný - a skontrolovať tieto údaje, ktoré tu má. Existuje obrovské množstvo pomlčiek, kdekoľvek od 3, 5, 6, 7 pomlčiek, dobre?

Takže, moja prvá myšlienka je, no, hej, je ľahké nájsť tú prvú -, však? = doľava alebo = STREDA HĽADISKA A2 a potom -, +1 v poriadku, ale aby som sa dostal k poslednému -, bude ma z toho bolieť hlava, nie, pretože, no, koľko čiarok máme? Mohli by sme zobrať SUBSTITUTE z A2, nahradiť čiarky, a porovnať jeho dĺžku, pôvodnú dĺžku. To mi hovorí počet pomlčiek, ale teraz viem, ktoré - nájsť 2., 3., 4., 5., ale použijem NÁJDIŤ?

Bol som pripravený ísť na VBA, však? To je moja reakcia na kolená. Povedal som, počkaj chvíľu. Povedal som, Anvar, v akej verzii programu Excel sa nachádzaš? Hovorí, že som v programe Excel 2016. Povedal som, to je nádherné. Ak používate program Excel 2013 alebo novší, mohli by sme použiť túto skvelú novú funkciu s názvom flash fill. S bleskovou výplňou mu musíme jednoducho dať vzor a ja mu dám dosť vzorov, takže nejde len o to, že si vezmem jednu s dvoma čiarkami a urobím to niekoľkokrát. Chcem sa uistiť, že mám takto niekoľko rôznych pomlčiek. Čad z tímu Excel vie, čo hľadám. Chad je človek, ktorý napísal logiku pre flash fill. Takže ich tam mám asi 3 a potom je klávesová skratka CONTROL + E skratka na použitie DATA a potom FLASH FILL a vyzerá to, že urobil správnu vec. Dobre, Mike.Pozrime sa, čo máte.

Mike: Vďaka, pán Excel. Áno. Blesková výplň vyhráva. Táto funkcia, flash fill, je jedným z moderných nástrojov Excelu, ktorý je jednoducho úžasný. Ak ide o jednorazovú dohodu a máte jednotný vzorec, hej, tak by som to urobil.

Hej, prejdime k ďalšiemu listu. Teraz môžeme namiesto použitia flash fill skutočne použiť power query. Teraz používam program Excel 2016, takže mám skupinu GET & TRANSFORM. To je sila. V starších verziách 2013 (do 10 - 2:30) si musíte v skutočnosti stiahnuť bezplatný doplnok power query.

Teraz, aby funkčný dopyt fungoval, je potrebné ho previesť do tabuľky programu Excel. Teraz by som znova použil bleskovú výplň, ak by išlo o jednorazovú dohodu. Kedy by ste použili dotaz na výkon? Ak by ste mali skutočne veľké dáta alebo prichádzali z externého zdroja, bola by to správna cesta, alebo by sa vám to mohlo páčiť lepšie, ako keby ste museli napísať 3 alebo 4 príklady pre flash fill, pretože pomocou power query môžeme konkrétne povedzme nájdi prvý - a nájdi posledný -.

Teraz to prevediem na tabuľku programu Excel. Mám vybranú jednu bunku, celé prázdne bunky. Idem na VLOŽIŤ, TABUĽKA, alebo použijete klávesnicu, CONTROL + T. Môžem kliknúť na OK alebo ENTER. Chcem pomenovať túto tabuľku, takže prejdem k TABUĽKOVÝM NÁSTROJOM, DIZAJNU, hore k VLASTNOSTÍM. Zavolám to STARTKEYTABLE a ENTER. Teraz sa môžem vrátiť k údajom DATA a preniesť ich do dotazu napájania pomocou tlačidla FROM TABLE. Tam je môj stĺpec. Je to meno. Nechcem si ponechať tento názov, pretože výstup bude exportovaný do programu Excel a chcem mu dať iný názov. Takže to nazvem CLEANEDKEYTABLE. Ten ZMENENÝ TYP nepotrebujem. Len pozerám na zdroj. Teraz môžem kliknúť na stĺpec a priamo v DOMOVI je tlačidlo SPLIT. Môžem povedať SPLIT, PODĽA ODMIEN. Vyzerá to, že už je to uhádnuté. Jaidem povedať ZĽAVA NAJVIAC. Kliknite na tlačidlo OK.

Now, if I look over here I see CHANGED TYPE. I don't need that so I'm going to get rid of that step. I only have SPLIT COLUMN BY DELIMITER. Now, I'm going to do this again but, instead of using the SPLIT button up here, right click down to SPLIT COLUMN, BY DELIMITER, and look at that. We can choose to split it by the RIGHT-MOST DELIMITER. Click OK. Now, I don't need these two columns so I'm going to right click the column I want to keep, REMOVE OTHER COLUMNS. I'm actually going to X this CHANGED TYPE out. It's going to say ARE YOU SURE YOU WANT TO DELETE THIS? I'm going to say, yes, DELETE. There's my clean data.

Now I can come up to CLOSE & LOAD. CLOSE & LOAD TO. This is the new IMPORT dialog box. It used to say LOAD TO but I want to load it to a table, on an EXISTING WORKSHEET. Click the collapse button. I'm going to select C1, uncollapse, click OK, and there we go. Power query to clean our data and get just the data we want. Alright. I'll throw it back to.

Bill: There’s the point right there, RIGHT-MOST DELIMITER in the SPLIT COLUMN BY DELIMITER, one of the cool features in power query. That's awesome.

Alright. My knee-jerk reaction -- VBA UDF (unintelligible - 05:34) really easy to do VBA. Switch over to ALT+F11. INSERT a MODULE. In that module, type this code. I'm going to (create a - 05:43) brand new function, I’m going to call it MIDPART, and I'm going to pass it some text, and then what I'm going to do is I'm going to go from the last character in that cell from the length of MYTEXT back to 1, STEP -1 and look at that character. So, the MID of MYTEXT, that variable i, tells us which character we're looking at for length of 1. Is it a -? As soon as I find a -, I'm going to take the LEFT of MYTEXT starting at character i - 1, so I get rid of everything for that last - all the way out, and then, make sure I don't go keep looking for more dashes, the EXIT FOR will get me out of this (unintelligible - 06:17) loop, and from there is the easy part. We're just going to take the MYTEXT, start at the MID of MYTEXT, (where I use the - 06:26) use the function FIND to find the first -, go 1 more than that, and return that back.

So, let's go back, ALT+Q, to return to Excel. = MIDPART tab of that, and it looks like it's working. Copy that down. Mike, do you have another one? (=MIDPart(A2))

Mike: Well, I do have another one,, but it's going to be one long formula -- not as short as that UDF. Alright, let's go over to the next sheet. Now, if we're going to do a formula and we have some text and there are always a different number of delimiters, somehow, I need to get the position of that last delimiter.

Now, this is going to take a few steps but I'm going to start with the SUBSTITUTE function. I'm going to look through that text, , the old text I want to find is in ”, that -, , and what do I want to put in its place or substitute? “”. That will put nothing in. Now, if I ) and CONTROL+ENTER, what is that going to do? (=SUBSTITUTE(A2,“-”,“”))

Well, now I can take the length of this and subtract it from the length of this item. That will tell me how many delimiters there are. F2, and right at the beginning, I'm going to type the length of that. That will give me the full length - the length of that dashless text, ), CONTROL+ENTER, double click, and send it down. that tells me how many delimiters there are for this text. There are 6. (=LEN(A2)-LEN(SUBSTITUTE(A2,“-”,“”)))

Now I'm going to use that sixth now inside of substitute to put a different character right at the sixth listing of the delimiter, F2, and if I type SUBSTITUTE, what we want to notice is this function has an instance number. If you look at other text functions like search and find, they don't have an instance number. Substitute is the only one I can think of that actually lets you specifically say which instance of a delimiter you want to deal with. Here's the text, ,. Old text is in “ a -, and I need to pick for the new text some character that will never be in this text ring. I'm going to choose, like, or something like that, , and that's where instance number comes in, ), CONTROL+ENTER, and there it is. If I double click and send it down, it's always putting that in the position of the last delimiter. (=SUBSTITUTE(A2,“-”,“^”,LEN(A2)-LEN(SUBSTITUTE(A2,“-”,“”))))

Now I need to figure out, in each one of these, what position it is in. F2. I'm going to use the SEARCH function. SEARCH. I type S and tab. Now, search and find are the same except for search is not case-sensitive. In this case, either one would be fine because the text I'm looking for is in “, that ^, ”, , within that text. By the way, the reason that I use search instead of find is because S tab gets me search but F I tab will get me find. So, it's like one character less when typing it out. CONTROL+ENTER, double click and send it down, and now it tells me, in the 27th position is that last delimiter. (=SEARCH(“^”,SUBSTITUTE(A2,“-”,“^”,LEN(A2)-LEN(SUBSTITUTE(A2,“-”,“”)))))

Now, I'm going to take this approach for these text items. I'm now going to use the left function and get everything from the very beginning all the way up to that position. That will get rid of that last little bit. Now, actually, search tells us 27 which is right there and we only want to go to 26. So, F2, and, at the end, I'm going to - 1, CONTROL+ENTER, double click and send it down. Now, I can use the left function. F2. LEFT. There it is, left of that, ,. That's how many characters. ), CONTROL+ENTER, double click and send it down. So, now, we have gotten rid of the last little bit after the last delimiter in every cell. (=LEFT(A2,SEARCH(“^”,SUBSTITUTE(A2,“-”,“^”,LEN(A2)-LEN(SUBSTITUTE(A2,“-”,“”))))-1))

Now all I need to do is replace the first four characters, first four characters, first three characters. Now, I can use the search function on the original text because it can find the - which is three and I'll tell replace, please go, from the first character, three characters in and replace it with nothing, F2, and right at the beginning, I'm going to type REPLACE. There's the old text. Now watch this. I want to give myself a little bit more breathing room. I'm just going to artificially pick a space, ALT+ENTER. That's kind of like we do in DAX. Now I just have more breathing room. That's the old text, ,. The starting number, I need to always start at the first position so I simply type 1, , and I need to find that first - which represents number of characters. So, S tab, “-” , through… within that text, that search will find 4, 4, 3. That will work. ) and then , new text “”. That will put nothing in those first characters. ). I have the entire column highlighted so I can populate this edited formula with CONTROL+ENTER, and there we go. All the way down, we’re extracting everything between the first and the last -. (=REPLACE(LEFT(A2,SEARCH(“^”,SUBSTITUTE(A2,“-”,“^”,LEN(A2)-LEN(SUBSTITUTE(A2,“-”,“”))))-1),1,SEARCH(“-”,A2),“”))

Now, the only reason we want to be crazy like that with formulas is if we wanted the formula result to instantly update whenever we changed anything, so if I type -00, instantly it updates. Power query and flash fill will not automatically update, alright? Send it back to.

Bill: Well, that was one heck of a formula. Like, substitute was the trick. I had used substitute in the first step but didn't see that it had the instance number. Alright, so, we have four different methods here today. My first method is flash fill. Select first few, select the blank box below that, and then CONTROL+E to flash fill. Mike's method, use power query. I love that, especially the split data letting you use the leftmost - and then the rightmost -. My live seminars always talk about this one feature. Should be a finalist for the Nobel Prize for the best excel feature. It wouldn't win but it would be in one of the top five, I'm sure. My method number three, VBA function, a UDF user-defined function, that iterates from the end of the cell, and then, Mike's method, the awesome formula method. Use substitute to find the location of the nth - and then pass that answer back into substitute that tells you which instance number to look from. Brilliant.

No a máš to. Chcem poďakovať všetkým, že sa zastavili. Uvidíme sa nabudúce pri ďalšom podcaste Dueling Excel od a ExcelIsFun.

Stiahnuť súbor

Stiahnutie vzorového súboru nájdete tu: Duel185.xlsm

Zaujímavé články...