Temukan Dash Terakhir - Tips Excel

Daftar Isi

Hari ini adalah pertanyaan gila. Anda memiliki kolom nomor bagian. Ada 4 sampai 7 tanda hubung di nomor bagian. Anda ingin mengekstrak hanya sebagian dari nomor bagian setelah tanda hubung pertama dan sampai tetapi tidak termasuk tanda hubung terakhir. Ini adalah episode duel Excel.

Menonton video

  • Tujuannya adalah menemukan tanda hubung pertama & terakhir dan menyimpan semua yang ada di antaranya
  • Bagian tersulit di sini adalah menemukan tanda hubung terakhir
  • Metode Tagihan 1: Isi Cepat
  • Isi beberapa yang pertama secara manual (termasuk beberapa dengan jumlah tanda hubung yang berbeda)
  • Pilih sel kosong di bawahnya
  • Ctrl + E untuk Mengisi Cepat
  • Metode Mike 2:
  • Gunakan Power Query
  • Di Excel 2016, Power Query berada dalam grup Dapatkan & Transformasi di Excel 2016
  • Di Excel 2010 & 2013, unduh Power Query dari Microsoft. Ini membuat tab Power Query baru di Pita
  • Ubah data Anda menjadi tabel menggunakan Ctrl + T
  • Gunakan Pisahkan Data di Power Query - pertama untuk memisahkan di tanda hubung paling kiri, lalu untuk memisahkan di tanda hubung paling kanan
  • Metode Tagihan 3:
  • Fungsi VBA yang mengulang dari ujung sel ke belakang untuk menemukan tanda hubung terakhir
  • Metode Mike 4:
  • Gunakan PENGGANTI untuk menemukan lokasi tanda hubung ke-N
  • SUBSTITUTE adalah satu-satunya fungsi teks yang memungkinkan Anda menentukan nomor Instance
  • Untuk menemukan nomor instance mana, gunakan =LEN(A2)-LEN(SUBSTITUTE)

Transkrip Video

Bill: Hei. Selamat datang kembali. Saatnya untuk podcast Dueling Excel lainnya. Saya Bill Jelen dari MrExcel. (Saya akan bergabung dengan Mike Girvin dari ExcelIsFun. Ini adalah - 00:03) episode 185: ekstrak dari yang pertama - hingga yang terakhir -.

Baik. Pertanyaan hari ini dikirim oleh Anvar di YouTube. Bagaimana saya bisa mengekstrak semuanya dari yang pertama - hingga yang terakhir -, dan memeriksa data yang dia miliki di sini. Ada sejumlah besar tanda hubung, mulai dari 3, 5, 6, 7 tanda hubung, oke?

Jadi, pikiran pertama saya adalah, hei, sangat mudah untuk menemukan yang pertama -, bukan? = left atau = MID dari FIND dari A2 dan kemudian -, +1 oke, tapi untuk sampai ke yang terakhir -, itu akan membuat kepalaku sakit, kan, karena, yah, berapa banyak garis yang kita punya? Kita bisa mengambil SUBSTITUTE dari A2, mengganti garis putus-putus, dan membandingkan panjangnya, panjang aslinya. Itu memberi tahu saya jumlah tanda hubung, tetapi sekarang saya tahu yang mana - yang harus dicari, yang ke-2, ke-3, ke-4, ke-5, tetapi apakah saya menggunakan FIND?

Saya sudah siap untuk pergi ke VBA, bukan? Itu reaksi spontan saya. Saya berkata, tunggu sebentar. Saya berkata, Anvar, Anda menggunakan versi Excel apa? Dia berkata, saya menggunakan Excel 2016. Saya berkata, itu indah. Jika Anda menggunakan Excel 2013 atau yang lebih baru, kami dapat menggunakan fitur baru yang hebat ini yang disebut flash fill. Dengan flash fill, kita hanya perlu memberikan pola, dan saya akan memberikan pola yang cukup sehingga tidak hanya saya mengambil satu dengan dua garis dan melakukannya beberapa kali. Saya ingin memastikan bahwa saya memiliki beberapa setrip berbeda dengan cara itu. Chad di tim Excel tahu apa yang saya cari. Chad adalah orang yang menulis logika untuk flash fill. Jadi, saya mendapatkan sekitar 3 dari mereka di sana dan kemudian CONTROL + E adalah jalan pintas untuk menggunakan DATA dan kemudian FLASH FILL, dan, tentu saja, sepertinya itu melakukan hal yang benar. Baiklah, Mike.Mari kita lihat apa yang Anda miliki.

Mike: Terima kasih, MrExcel. Ya. Pengisian flash menang. Fitur tersebut, flash fill, adalah salah satu alat Excel modern yang sangat menakjubkan. Jika ini kesepakatan satu kali dan Anda memiliki pola yang konsisten, hei, itulah cara saya melakukannya.

Hei, mari kita ke lembar berikutnya. Sekarang, alih-alih menggunakan flash fill, kita sebenarnya bisa menggunakan power query. Sekarang, saya menggunakan Excel 2016 jadi saya memiliki grup GET & TRANSFORM. Itulah permintaan kekuatan. Di versi sebelumnya, 2013 (hingga 10 - 2:30), Anda sebenarnya harus mengunduh add-in kueri daya gratis.

Sekarang, untuk mendapatkan kekuatan query untuk bekerja, ini harus diubah menjadi tabel Excel. Sekarang, sekali lagi, saya akan menggunakan flash fill jika ini adalah kesepakatan satu kali. Kapan Anda akan menggunakan power query? Nah, jika Anda memiliki data yang sangat besar atau berasal dari sumber eksternal, ini akan menjadi cara yang tepat, atau Anda bahkan mungkin menyukai ini lebih baik daripada harus mengetikkan 3 atau 4 contoh untuk flash fill karena, dengan power query, kita bisa secara khusus mengatakan temukan yang pertama - dan temukan yang terakhir -.

Sekarang, saya akan mengubahnya menjadi tabel Excel. Saya memiliki satu sel yang dipilih, sel-sel kosong di sekelilingnya. Saya pergi ke INSERT, TABLE, atau Anda menggunakan keyboard, CONTROL + T. Saya bisa klik OK atau ENTER. Saya ingin memberi nama tabel ini, jadi saya akan pergi ke ALAT TABEL, DESAIN, hingga SIFAT. Saya akan menyebutnya STARTKEYTABLE dan ENTER. Sekarang saya dapat kembali ke DATA, membawanya ke kueri daya menggunakan tombol DARI TABEL. Itu kolom saya. Ada namanya. Saya tidak ingin menggunakan nama ini karena hasilnya akan diekspor ke Excel dan saya ingin memberinya nama yang berbeda. Jadi, saya akan menyebutnya CLEANEDKEYTABLE. Saya tidak membutuhkan JENIS BERUBAH itu. Saya hanya melihat sumbernya. Sekarang saya dapat mengklik kolom dan, tepat di HOME, ada tombol SPLIT. Saya bisa bilang SPLIT, BY DELIMITER. Sepertinya sudah bisa ditebak. SAYA'Saya akan mengatakan PALING KIRI. Klik 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.

Nah, begitulah. Saya ingin berterima kasih kepada semua orang karena telah mampir. Sampai jumpa di lain waktu untuk podcast Dueling Excel lainnya dari dan ExcelIsFun.

Unduh berkas

Unduh file sampel di sini: Duel185.xlsm

Artikel yang menarik...