VBA HITTA NÄSTA - Hur använder jag FindNext-funktionen i Excel VBA?

Innehållsförteckning

Excel VBA Hitta nästa

Som i excel när vi trycker på CTRL + F dyker en guiden upp som låter oss söka efter ett värde i det angivna kalkylbladet och när värdet hittats klickar vi på hitta bredvid för att hitta det andra liknande värdet, eftersom det är en kalkylfunktion kan också använda den i VBA som Application-egenskapsmetod som application.findnext för samma ändamål.

Att hitta det specifika värdet i det nämnda intervallet är bra, men tänk om kravet är att hitta värdet med flera förekomster. I en av de tidigare artiklarna har vi diskuterat "Hitta" -metoden i VBA, och den är inte komplicerad alls men att hitta alla repetitiva händelser är endast möjlig med "Hitta nästa" -metoden i excel VBA.

I den här artikeln visar vi dig hur du använder den här "Hitta nästa" i Excel VBA.

Vad är Hitta nästa i Excel VBA?

Som ordet säger betyder "Hitta nästa" från att den hittade cellen fortsätter att söka efter nästa värde tills den återgår till den ursprungliga cellen där vi har startat sökningen.

Detta är den avancerade versionen av "Find" -metoden, som bara söker en gång det nämnda värdet i det nämnda intervallet.

Nedan visas syntaxen för FINN NEXT-metoden i Excel VBA.

Efter: Det är ordet som vi letar efter.

Exempel på Hitta nästa metod i Excel VBA

Nedan följer exemplen på att hitta nästa metod i excel VBA.

Titta till exempel på nedanstående data.

Steg 1 - I den här informationen måste vi hitta stadsnamnet "Bangalore." Låt oss starta delproceduren i den grundläggande visuella redigeraren.

Koda:

Sub RangeNext_Example () End Sub

Steg # 2 - Förklara först variabeln som "Range" -objekt.

Koda:

Sub RangeNext_Example () Dim Rng As Range End Sub

Steg # 3 - Ställ in referensen för objektvariabeln som "Range (" A2: A11 ").

Koda:

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

Eftersom våra uppgifter om stadslistan finns där i området celler från A2 till A11 i detta intervall, är det bara vi som ska söka efter staden "Bangalore."

Eftersom vi ställer in områdesreferensen till variabeln "Rng" använder vi denna variabel istället för att använda RANGE ("A2: A11") varje gång.

Steg 4 - Använd RNG-variabeln och öppna Find-metoden.

Koda:

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

Steg # 5 - Det första argumentet för FIND-metoden är "Vad", dvs vad vi försöker söka i det nämnda intervallet, så värdet vi söker är "Bangalore."

Koda:

Sub RangeNext_Example () Dim Rng As Range Set Rng = Range ("A2: A12") Rng.Find What: = "Bangalore" End Sub

Steg # 6 - För att visa i vilken cell vi har hittat det här värdet, förklara en variabel till som en sträng.

Koda:

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

Steg # 7 - Tilldela den hittade celladressen för den här variabeln.

Koda:

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.Adress End Sub Sub
Obs: RNG. Adress eftersom RNG kommer att ha referensen för det hittade värdecellen.

Steg # 8 - Visa nu det tilldelade resultatet för celladressvariabel i meddelandefältet i 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 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

Steg # 21 - Detta kommer att fortsätta visa alla matchande celladresser, och till slut kommer det att visa meddelandet "Sökningen är över" i den nya meddelandefältet.

Saker att komma ihåg

  • HITTA-metoden kan bara hitta ett värde åt gången.
  • HITTA NÄSTA i excel VBA kan hitta nästa värde från den redan hittade värdecellen.
  • Använd Do While-slingan för att slinga igenom alla celler i intervallet.

Intressanta artiklar...