VBA CARI SETERUSNYA - Bagaimana Menggunakan Fungsi FindNext di Excel VBA?

Excel VBA Cari Seterusnya

Seperti di excel ketika kita menekan CTRL + F kotak wizard muncul yang membolehkan kita mencari nilai di lembaran kerja yang diberikan dan setelah nilai dijumpai, kita klik di cari di sebelah untuk mencari nilai lain yang serupa, kerana ia adalah ciri lembaran kerja kita juga boleh menggunakannya dalam VBA sebagai kaedah harta tanah Application sebagai application.findnext untuk tujuan yang sama.

Mencari nilai tertentu dalam julat yang disebutkan itu baik-baik saja, tetapi bagaimana jika syaratnya adalah mencari nilai dengan banyak kejadian. Dalam salah satu artikel sebelumnya, kami telah membincangkan kaedah "Cari" di VBA, dan sama sekali tidak rumit tetapi mencari semua kejadian berulang hanya mungkin dengan kaedah "Cari Seterusnya" di excel VBA.

Dalam artikel ini, kami akan menunjukkan kepada anda cara menggunakan "Find Next" ini di Excel VBA.

Apa itu Find Next di Excel VBA?

Seperti kata kata, "Cari Seterusnya" bermaksud dari sel yang dijumpai terus mencari nilai seterusnya hingga kembali ke sel asal di mana kita telah memulakan pencarian.

Ini adalah versi lanjutan dari kaedah "Cari", yang hanya mencari satu kali nilai yang disebutkan dalam julat yang disebutkan.

Berikut adalah sintaks kaedah FIND NEXT dalam Excel VBA.

Selepas: Ini adalah perkataan yang kita cari.

Contoh Kaedah Cari Seterusnya dalam Excel VBA

Berikut adalah Contoh kaedah mencari seterusnya dalam excel VBA.

Contohnya, lihat data di bawah.

Langkah # 1 - Dalam data ini, kita perlu mencari nama bandar "Bangalore." Mari mulakan subproses dalam penyunting visual asas.

Kod:

Sub RangeNext_Contoh () End Sub

Langkah # 2 - Pertama, nyatakan pemboleh ubah sebagai objek "Range".

Kod:

Sub RangeNext_Contoh () Dim Rng Sebagai Range End Sub

Langkah # 3 - Tetapkan rujukan untuk pemboleh ubah objek sebagai "Range (" A2: A11 ").

Kod:

Sub RangeNext_Contoh () Dim Rng Sebagai Range Set Rng = Range ("A2: A12") End Sub

Oleh kerana data senarai bandar kami ada dalam sel dari A2 hingga A11 dalam julat ini, hanya kami yang akan mencari kota "Bangalore."

Oleh kerana kami menetapkan rujukan rentang ke pemboleh ubah "Rng", kami menggunakan pemboleh ubah ini dan bukannya menggunakan RANGE ("A2: A11") setiap waktu.

Langkah # 4 - Gunakan pemboleh ubah RNG dan buka kaedah Cari.

Kod:

Sub RangeNext_Contoh () Dim Rng Sebagai Range Set Rng = Range ("A2: A12") Rng.Cari Akhir Sub

Langkah # 5 - Argumen pertama kaedah FIND adalah "Apa", iaitu apa yang kita cari dalam julat yang disebutkan, jadi nilai yang kita cari adalah "Bangalore."

Kod:

Sub RangeNext_Contoh () Dim Rng Sebagai Range Set Rng = Range ("A2: A12") Rng.Cari Apa: = "Bangalore" End Sub

Langkah # 6 - Untuk menunjukkan di mana sel kita dapati nilai ini menyatakan satu lagi pemboleh ubah sebagai rentetan.

Kod:

Sub RangeNext_Contoh () Dim Rng Sebagai Range Dim CellAdderess Sebagai String Set Rng = Range ("A2: A12") Rng.Cari Apa: = "Bangalore" End Sub

Langkah # 7 - Untuk pemboleh ubah ini, tetapkan alamat sel yang dijumpai.

Kod:

Sub RangeNext_Contoh () Dim Rng Sebagai Range Dim CellAdderess Sebagai String Set Rng = Range ("A2: A12"). Find (What: = "Bangalore") Rng.Cari Apa: = "Bangalore" CellAddress = Rng.Address End Sub
Nota: RNG. Alamat kerana RNG akan mempunyai rujukan untuk sel nilai yang dijumpai.

Langkah # 8 - Sekarang tunjukkan hasil pemboleh ubah alamat sel yang ditentukan di kotak mesej di VBA.

Sub RangeNext_Example() Dim Rng As Range Dim CellAdderess As String Set Rng = Range("A2:A12").Find(What:="Bangalore") Rng.Find What:="Bangalore" CellAddress = Rng.Address MsgBox CellAddress End Sub

Step#9 - Run the code and see what we get here.

So we have found the value “Bangalore” in the cell A5. With the Find method, we can find only one cell, so instead of FIND, we need to use FIND NEXT in excel VBA.

Step#10 - We need to reference the range object variable but by using the FIND NEXT method in excel VBA.

Code:

Sub RangeNext_Example() Dim Rng As Range Dim CellAdderess As String Set Rng = Range("A2:A12").Find(What:="Bangalore") Rng.Find What:="Bangalore" CellAddress = Rng.Address MsgBox CellAddress Set Rng = Range("A2:A12").FindNext(Rng) End Sub

As you can see above, we have used the VBA FIND NEXT method, but inside the function, we have used a range object variable name.

Step#11 - Now again, assign the cell address and show the address in the message box.

Code:

Sub RangeNext_Example() Dim Rng As Range Dim CellAdderess As String Set Rng = Range("A2:A12").Find(What:="Bangalore") Rng.Find What:="Bangalore" CellAddress = Rng.Address MsgBox CellAddress Set Rng = Range("A2:A12").FindNext(Rng) CellAddress = Rng.Address MsgBox CellAddress End Sub

Step#12 - Run the macro and see what we get in the first message box.

Step#13 - The first message box shows the value “Bangalore” found in the cell A5. Click on the Ok button to see the next found value.

The second value found in A7 cell, press Ok to continue.

VBA Find Next (Using Loop)

It will exit the VBA subprocedure, but we are one more to be found in cell A10. When the values are to be found in more than one cell, then it is a better idea to use loops.

In this case, too, we have value “Bangalore” in more than one cell, so we need to include loops here.

Step#14 - First, declare two variables as the range.

Code:

Sub RangeNext_Example1() Dim Rng As Range Dim FindRng As Range End Sub

Step#15 - Set the reference for the first variable, as shown below.

Code:

Sub RangeNext_Example1() Dim Rng As Range Dim FindRng As Range Set Rng = Range("A2:A11").Find(What:="Bangalore") End Sub

Step#16 - For the second variable, set the reference by using the FIND VBA function.

Sub RangeNext_Example1() Dim Rng As Range Dim FindRng As Range Set Rng = Range("A2:A11").Find(What:="Bangalore") Set FindRng = Rng.FindNext("Bangalore") End Sub

Step#17 - Before we start searching for the value, we need to identify from which cell we are starting the search, for that declares the variable as a string.

Code:

Sub RangeNext_Example1() Dim Rng As Range Dim FindRng As Range Set Rng = Range("A2:A11").Find(What:="Bangalore") Set FindRng = Rng.FindNext("Bangalore") Dim FirstCell As String FirstCell = Rng.Address End Sub

Step#18 - For this variable, assign the first cell address.

Code:

Sub RangeNext_Example1() Dim Rng As Range Dim FindRng As Range Set Rng = Range("A2:A11") Set FindRng = Rng.Find(What:="Bangalore") Dim FirstCell As String FirstCell = Rng.Address End Sub

Step#19 - Now, we need to include the “Do While” loop to loop through all the cells and find the searching value.

Code:

Sub RangeNext_Example1() Dim Rng As Range Dim FindRng As Range Set Rng = Range("A2:A11").Find(What:="Bangalore") Set FindRng = Rng.FindNext("Bangalore") Dim FirstCell As String FirstCell = Rng.Address Do Loop While FirstCell Cell.Address End Sub

Inside the loop, mention the message box and VBA FIND NEXT method.

Step#20 - Below is the complete code for you.

Code:

Sub FindNext_Example () Dim FindValue Sebagai String FindValue = "Bangalore" Dim Rng Sebagai Range Set Rng = Range ("A2: A11") Dim FindRng Sebagai Range Set FindRng = Rng.Cari (Apa: = FindValue) Dim FirstCell Sebagai String FirstCell = FindRng.Address Do MsgBox FindRng.Address Set FindRng = Rng.FindNext (FindRng) Loop Semasa FirstCell FindRng.Address MsgBox "Pencarian selesai" End Sub

Langkah # 21 - Ini akan terus menunjukkan semua alamat sel yang sepadan, dan pada akhirnya, ia akan menunjukkan pesan sebagai "Pencarian Berakhir" di kotak pesan baru.

Perkara yang Perlu Diingat

  • Kaedah MENCARI hanya dapat mencari satu nilai pada satu masa.
  • TEMUKAN SETERUSNYA di excel VBA dapat mencari nilai seterusnya dari sel nilai yang sudah dijumpai.
  • Gunakan gelung Do While untuk melancarkan semua sel dalam julat.

Artikel menarik...