Pronađi posljednju crticu - Excel savjeti

Sadržaj

Danas je ludo pitanje. Imate stupac s brojevima dijelova. U dijelu broja ima od 4 do 7 crtica. Želite izvući samo dio broja dijela nakon prve crtice i do, ali ne uključujući posljednju crticu. Ovo je dvobojna Excel epizoda.

Gledaj video

  • Cilj je pronaći prvu i zadnju crticu i zadržati sve između
  • Ovdje je najteže pronaći posljednju crticu
  • Metoda računa 1: Flash Fill
  • Ručno ispunite prvih nekoliko (uključujući neke s različitim brojem crtica)
  • Odaberite praznu ćeliju ispod toga
  • Ctrl + E za bljeskanje
  • Mike metoda 2:
  • Koristite Power Query
  • U programu Excel 2016, Power Query nalazi se u grupi Dohvati i transformiraj u programu Excel 2016
  • U Excelu 2010 i 2013 preuzmite Power Query s Microsofta. Stvara novu karticu Power Query na vrpci
  • Pretvorite podatke u tablicu pomoću Ctrl + T
  • Upotrijebite Podijeljeni podaci u Power Queryju - prvo da biste podijelili na krajnjoj lijevoj crtici, a zatim da biste podijelili na krajnjoj desnoj crtici
  • Metoda računa 3:
  • VBA funkcija koja se ponavlja od kraja ćelije prema natrag kako bi pronašla zadnju crticu
  • Mike metoda 4:
  • Upotrijebite SUBSTITUTE da biste pronašli mjesto N-te crtice
  • ZAMJENA je jedina tekstualna funkcija koja vam omogućuje da odredite broj instance
  • Da biste pronašli broj instance, upotrijebite =LEN(A2)-LEN(SUBSTITUTE)

Prijepis videozapisa

Bill: Hej. Dobrodošao natrag. Vrijeme je za još jedan Ducast Excel podcast. Ja sam Bill Jelen iz MrExcela. (Pridružit će mi se Mike Girvin iz programa ExcelIsFun. Ovo je naša - 00:03) epizoda 185: izvod iz prve - do posljednje -.

U redu. Današnje pitanje Anvar šalje na YouTubeu. Kako mogu izdvojiti sve od prvog - do posljednjeg - i provjeriti ove podatke koje on ovdje ima. Postoji ogroman broj crtica, bilo od 3, 5, 6, 7 crtica, u redu?

Dakle, moja prva misao je, pa, hej, stvarno je lako pronaći prvu - zar ne? = lijevo ili = SREDINA PRONALAZA A2, a zatim -, +1 u redu, ali da dođem do zadnjeg -, to će me boljeti u glavi, zar ne, jer, koliko crtica imamo? Mogli bismo uzeti ZAMJENU A2, zamjenjujući crtice, i usporediti duljinu te, izvorne duljine. To mi govori broj crtica, ali sada znam koje - pronaći, 2., 3., 4., 5., ali koristim li FIND?

Bila sam spremna otići na VBA, zar ne? To je moja reakcija trzaja koljena. Rekao sam, pričekaj malo. Rekao sam, Anvar, u kojoj si verziji Excela? Kaže, ja sam u Excelu 2016. Rekao sam, to je prelijepo. Ako koristite Excel 2013 ili noviju verziju, mogli bismo upotrijebiti ovu sjajnu novu značajku koja se naziva flash fill. S flash fill-om jednostavno mu moramo dati uzorak, a ja ću mu dati dovoljno uzorka, tako da nije samo da uzmem jedan s dvije crtice i to nekoliko puta. Želim biti siguran da na taj način imam nekoliko različitih crtica. Chad iz Excel-ovog tima zna što tražim. Chad je tip koji je napisao logiku za flash fill. Dakle, uđem tamo otprilike 3, a zatim je CONTROL + E prečac za upotrebu PODATAKA, a zatim FLASH FILL i, sasvim sigurno, čini se da je učinio pravu stvar. U redu, Mike.Da vidimo što imate.

Mike: Hvala, MrExcel. Da. Flash ispunjavanje pobjeđuje. Ta je značajka upravo ovdje, flash fill, jedan od modernih Excelovih alata koji je jednostavno nevjerojatan. Ako se radi o jednokratnom dogovoru i ako imate dosljedan obrazac, hej, to bih ja na taj način učinio.

Hej, idemo na sljedeći list. Sada, umjesto da koristimo flash ispunjavanje, zapravo možemo koristiti power power upit. Sada koristim Excel 2016 pa imam grupu GET & TRANSFORM. To je upit snage. U ranijim verzijama, 2013. (do 10 - 2.30), zapravo morate preuzeti dodatak za besplatni upit za napajanje.

Sada, da bi upit za napajanje funkcionirao, to se mora pretvoriti u Excel tablicu. Sada bih opet koristio flash fill ako je ovo jednokratna ponuda. Kada biste koristili upit snage? Pa, ako ste imali stvarno velike podatke ili ste dolazili iz vanjskog izvora, ovo bi bio put ili bi vam se ovo možda možda svidjelo bolje nego da trebate utipkati 3 ili 4 primjera za flash punjenje jer, s upitom napajanja, možemo konkretno recite pronađi prvo - i pronađi posljednje -.

Sada ću ovo pretvoriti u Excel tablicu. Imam jednu odabranu ćeliju, prazne ćelije do kraja. Idem na INSERT, TABLE ili koristite tipkovnicu CONTROL + T. Mogu kliknuti OK ili ENTER. Želim imenovati ovu tablicu, pa ću prijeći na TABLE ALATI, DIZAJN, u SVOJSTVA. Nazvat ću ovo STARTKEYTABLE i ENTER. Sada se mogu vratiti na DATA, unijeti ga u upit napajanja pomoću gumba FROM TABLE. Tu je moja kolumna. Eto imena. Ne želim zadržati ovo ime jer će se izlaz izvesti u Excel i želim mu dati drugo ime. Dakle, nazvat ću ga ČISTIM KLJUČNIM STOLOM. Ne treba mi taj PROMJENJENI TIP. Samo gledam izvor. Sada mogu kliknuti na stupac i, gore u HOME, nalazi se gumb PODIJELI. Mogu reći PODJELA, DELIMEROM. Izgleda da je već pretpostavljeno. Ja 'reći ću LIJEVO-NAJVIŠE. Kliknite U redu.

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.

Pa, eto. Želim zahvaliti svima što su navratili. Vidjet ćemo se sljedeći put za još jedan Ducast Excel podcast od i ExcelIsFun.

Preuzmi datoteku

Preuzmite uzorak datoteke ovdje: Duel185.xlsm

Zanimljivi članci...