VBA ZNAJDŹ NASTĘPNY - Jak korzystać z funkcji FindNext w Excel VBA?

Excel VBA Znajdź następny

Podobnie jak w programie Excel, gdy naciskamy CTRL + F, pojawia się okno kreatora, które pozwala nam przeszukać wartość w danym arkuszu, a po znalezieniu wartości klikamy znajdź obok, aby znaleźć inną podobną wartość, ponieważ jest to funkcja arkusza roboczego, którą można go również używać w VBA jako metody właściwości aplikacji jako application.findnext do tych samych celów.

Znalezienie określonej wartości w wymienionym zakresie jest w porządku, ale co, jeśli wymaganiem jest znalezienie wartości z wieloma wystąpieniami. W jednym z wcześniejszych artykułów omówiliśmy metodę „Znajdź” w języku VBA i nie jest to wcale skomplikowane, ale znalezienie wszystkich powtarzających się wystąpień jest możliwe tylko za pomocą metody „Znajdź następny” w programie Excel VBA.

W tym artykule pokażemy, jak korzystać z funkcji „Znajdź następny” w programie Excel VBA.

Co to jest Znajdź następny w Excel VBA?

Jak mówi słowo „Znajdź następny” oznacza, że ​​od znalezionej komórki kontynuuj wyszukiwanie następnej wartości, aż wróci do pierwotnej komórki, w której rozpoczęliśmy wyszukiwanie.

Jest to zaawansowana wersja metody „Znajdź”, która wyszukuje tylko jeden raz podaną wartość w podanym zakresie.

Poniżej znajduje się składnia metody ZNAJDŹ NASTĘPNY w Excel VBA.

Po: To jest słowo, którego szukamy.

Przykłady metody Znajdź następny w Excel VBA

Poniżej znajdują się przykłady znajdowania następnej metody w programie Excel VBA.

Na przykład spójrz na poniższe dane.

Krok # 1 - W tych danych musimy znaleźć nazwę miasta „Bangalore”. Rozpocznijmy podprocedurę w podstawowym edytorze wizualnym.

Kod:

Sub Range Next_Example () End Sub

Krok # 2 - Najpierw zadeklaruj zmienną jako obiekt „Zakres”.

Kod:

Sub RangeNext_Example () Dim Rng As Range End Sub

Krok # 3 - Ustaw odniesienie dla zmiennej obiektu jako „Zakres („ A2: A11 ”).

Kod:

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

Ponieważ nasze dane listy miast znajdują się w zakresie komórek od A2 do A11 w tym zakresie, tylko my będziemy szukać miasta „Bangalore”.

Ponieważ ustawiliśmy odniesienie do zakresu na zmienną „Rng”, używamy tej zmiennej zamiast używania RANGE („A2: A11”) za każdym razem.

Krok # 4 - Użyj zmiennej RNG i otwórz metodę Find.

Kod:

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

Krok # 5 - Pierwszym argumentem metody FIND jest „Co”, czyli to, co staramy się wyszukać we wspomnianym zakresie, więc szukana wartość to „Bangalore”.

Kod:

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

Krok # 6 - Aby pokazać, w której komórce znaleźliśmy tę wartość, zadeklaruj jeszcze jedną zmienną jako ciąg.

Kod:

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

Krok # 7 - W przypadku tej zmiennej przypisz znaleziony adres komórki.

Kod:

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 End Sub
Uwaga: RNG. Adres, ponieważ RNG będzie miał odwołanie do znalezionej komórki wartości.

Krok # 8 - Teraz pokaż przypisaną zmienną adresu komórki w oknie komunikatu w 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 "Wyszukiwanie zakończone" End Sub

Krok # 21 - Spowoduje to dalsze wyświetlanie wszystkich pasujących adresów komórek, a na końcu wyświetli komunikat „Wyszukiwanie zakończone” w nowym oknie komunikatu.

Rzeczy do zapamiętania

  • FIND może znaleźć tylko jedną wartość naraz.
  • ZNAJDŹ NASTĘPNY w programie Excel VBA może znaleźć następną wartość z już znalezionej komórki wartości.
  • Użyj pętli Do While, aby przejrzeć wszystkie komórki w zakresie.

Interesujące artykuły...