VBA FIND NEXT - Come utilizzare la funzione FindNext in Excel VBA?

Excel VBA Trova successivo

Come in Excel, quando premiamo CTRL + F viene visualizzata una finestra della procedura guidata che ci consente di cercare un valore nel foglio di lavoro specificato e una volta trovato il valore, facciamo clic su trova accanto per trovare l'altro valore simile, poiché è una funzionalità del foglio di lavoro che noi può anche usarlo in VBA come metodo di proprietà dell'applicazione come application.findnext per gli stessi scopi.

Trovare il valore specifico nell'intervallo menzionato va bene, ma cosa succede se il requisito è trovare il valore con più occorrenze. In uno degli articoli precedenti, abbiamo discusso il metodo "Trova" in VBA, e non è affatto complesso, ma trovare tutte le occorrenze ripetitive è possibile solo con il metodo "Trova successivo" in Excel VBA.

In questo articolo, ti mostreremo come utilizzare questo "Trova successivo" in Excel VBA.

Cos'è Trova successivo in Excel VBA?

Come dice la parola, "Trova successivo" significa che dalla cella trovata continuare a cercare il valore successivo fino a quando non si ritorna alla cella originale in cui abbiamo iniziato la ricerca.

Questa è la versione avanzata del metodo "Trova", che cerca solo una volta il valore menzionato nell'intervallo menzionato.

Di seguito è riportata la sintassi del metodo FIND NEXT in Excel VBA.

Dopo: è la parola che stiamo cercando.

Esempi di metodo Trova successivo in Excel VBA

Di seguito sono riportati gli esempi per trovare il metodo successivo in Excel VBA.

Ad esempio, guarda i dati seguenti.

Passaggio 1: in questi dati, dobbiamo trovare il nome della città "Bangalore". Iniziamo la sottoprocedura nell'editor visuale di base.

Codice:

Sub RangeNext_Example () End Sub

Passo # 2 - In primo luogo, dichiarare la variabile come oggetto "Range".

Codice:

Sub RangeNext_Example () Dim Rng As Range End Sub

Step # 3 - Imposta il riferimento per la variabile oggetto come "Range (" A2: A11 ").

Codice:

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

Poiché i nostri dati dell'elenco delle città sono presenti nell'intervallo di celle da A2 a A11 in questo intervallo, solo noi cercheremo la città "Bangalore".

Poiché impostiamo il riferimento dell'intervallo sulla variabile "Rng", utilizziamo questa variabile invece di utilizzare RANGE ("A2: A11") ogni volta.

Passaggio 4: utilizzare la variabile RNG e aprire il metodo Find.

Codice:

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

Passaggio 5: il primo argomento del metodo TROVA è "Cosa", ovvero ciò che stiamo cercando di cercare nell'intervallo indicato, quindi il valore che stiamo cercando è "Bangalore".

Codice:

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

Passaggio # 6 - Per mostrare in quale cella abbiamo trovato questo valore, dichiara un'altra variabile come stringa.

Codice:

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

Passaggio 7: per questa variabile, assegna l'indirizzo della cella trovato.

Codice:

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
Nota: RNG. Indirizzo perché RNG avrà il riferimento per la cella del valore trovato.

Passaggio 8: ora mostra il risultato della variabile dell'indirizzo di cella assegnato nella finestra del messaggio in 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 Esegui MsgBox FindRng.Address Imposta FindRng = Rng.FindNext (FindRng) Ciclo mentre FirstCell FindRng.Address MsgBox "La ricerca è finita" End Sub

Passaggio 21: continuerà a mostrare tutti gli indirizzi di cella corrispondenti e, alla fine, mostrerà il messaggio come "La ricerca è finita" nella nuova finestra di messaggio.

Cose da ricordare

  • Il metodo TROVA può trovare un solo valore alla volta.
  • TROVA SUCCESSIVO in Excel VBA può trovare il valore successivo dalla cella del valore già trovato.
  • Utilizza il ciclo Do While per scorrere tutte le celle nell'intervallo.

Articoli interessanti...