VBA GĂSEȘTE NEXT | Cum se folosește funcția FindNext în Excel VBA?

Excel VBA Găsiți următorul

La fel ca în Excel când apăsăm CTRL + F apare o casetă de expert care ne permite să căutăm o valoare în foaia de lucru dată și odată ce valoarea este găsită, facem clic pe Găsiți lângă pentru a găsi cealaltă valoare similară, deoarece este o caracteristică a foii de lucru îl poate folosi și în VBA ca metodă de proprietate a aplicației ca application.findnext în aceleași scopuri.

Găsirea valorii specifice în intervalul menționat este în regulă, dar dacă cerința este de a găsi valoarea cu mai multe apariții. Într-unul dintre articolele anterioare, am discutat despre metoda „Găsiți” în VBA și nu este deloc complexă, dar găsirea tuturor aparițiilor repetitive este posibilă numai cu metoda „Găsiți următorul” în excel VBA.

În acest articol, vă vom arăta cum să utilizați acest „Găsește următorul” în Excel VBA.

Ce este Find Next în Excel VBA?

După cum spune cuvântul „Găsește următorul” înseamnă din celula găsită continuați să căutați următoarea valoare până când reveniți la celula originală unde am început căutarea.

Aceasta este versiunea avansată a metodei „Găsiți” care caută o singură dată valoarea menționată în intervalul menționat.

Mai jos este sintaxa metodei FIND NEXT în Excel VBA.

După: Este cuvântul pe care îl căutăm.

Exemple de metode Găsește Următorul în Excel VBA

Mai jos sunt exemplele de metode de găsire următoare în Excel VBA.

De exemplu, uitați-vă la datele de mai jos.

Puteți descărca acest șablon VBA Find Next Excel aici - VBA Find Next Excel Template

Pasul 1 - În aceste date, trebuie să găsim numele orașului „Bangalore”. Să începem subprocedura în editorul de bază vizual.

Cod:

 Sub RangeNext_Example () End Sub 

Pasul 2 - Mai întâi, declarați variabila ca obiect „Range”.

Cod:

 Sub RangeNext_Example () Dim Rng As Range End Sub 

Pasul 3 - Setați referința pentru variabila obiect ca „Range („ A2: A11 ”).

Cod:

 Sub RangeNext_Example () Dim Rng As Range Set Rng = Range ("A2: A12") End Sub 

Deoarece datele noastre din lista orașelor se află în intervalul de celule de la A2 la A11 în acest interval, doar vom căuta orașul „Bangalore”.

Deoarece stabilim referința intervalului la variabila „Rng”, folosim această variabilă în loc să folosim RANGE („A2: A11”) de fiecare dată.

Pasul 4 - Utilizați variabila RNG și deschideți metoda Găsiți.

Cod:

 Sub RangeNext_Example () Dim Rng As Range Set Rng = Range ("A2: A12") Rng.Găsește End Sub 

Pasul 5 - Primul argument al metodei FIND este „Ce”, adică ceea ce încercăm să căutăm în intervalul menționat, deci valoarea pe care o căutăm este „Bangalore”.

Cod:

 Sub RangeNext_Example () Dim Rng As Range Set Rng = Range ("A2: A12") Rng.Găsiți ce: = "Bangalore" End Sub 

Pasul 6 - Pentru a arăta în ce celulă am găsit această valoare, declarăm încă o variabilă ca un șir.

Cod:

 Sub RangeNext_Example () Dim Rng As Range Dim CellAdderess As String Set Rng = Range ("A2: A12") Rng.Găsiți ce: = "Bangalore" End Sub 

Pasul 7 - Pentru această variabilă atribuiți adresa de celulă găsită.

Cod:

 Sub RangeNext_Example () Dim Rng As Range Dim CellAdderess As String Set Rng = Range ("A2: A12"). Găsiți (Ce: = "Bangalore") Rng.Găsiți Ce: = "Bangalore" CellAddress = Rng.Address End Sub 

Notă: RNG.Adresă, deoarece RNG va avea referința pentru celula de valoare găsită.

Pasul 8 - Acum arată rezultatul variabilei de adresă de celulă atribuit în caseta de mesaj din VBA.

 Sub RangeNext_Example () Dim Rng As Range Dim CellAdderess As String Set Rng = Range ("A2: A12"). Găsiți (Ce: = "Bangalore") Rng.Găsiți Ce: = "Bangalore" CellAddress = Rng.Address MsgBox CellAddress End Sub 

Pasul # 9 - Rulați codul și vedeți ce obținem aici.

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 As String FindValue = "Bangalore" Dim Rng As Range Set Rng = Range("A2:A11") Dim FindRng As Range Set FindRng = Rng.Find(What:=FindValue) Dim FirstCell As String FirstCell = FindRng.Address Do MsgBox FindRng.Address Set FindRng = Rng.FindNext(FindRng) Loop While FirstCell  FindRng.Address MsgBox "Search is over" End Sub 

Step#21 – This will keep showing all the matching cell address and in the end, it will show the message as “Search is Over” in the new message box.

Things to Remember

  • FIND method can find only one value at a time.
  • FIND NEXT in excel VBA can find the next value from the already found value cell.
  • Use Do While loop to loop through all the cells in the range.