VBA FIND NEXT | Excel VBAでFindNext関数を使用する方法は?

ExcelVBA次を検索

Excelのように、CTRL + Fを押すと、ウィザードボックスがポップアップし、指定されたワークシートの値を検索できます。値が見つかったら、[次を検索]をクリックして、他の同様の値を見つけます。これは、ワークシートの機能です。同じ目的で、アプリケーションプロパティメソッドとしてapplication.findnextとしてVBAで使用することもできます。

上記の範囲で特定の値を見つけることは問題ありませんが、要件が複数回出現する値を見つけることである場合はどうでしょうか。以前の記事の1つで、VBAの「検索」メソッドについて説明しましたが、複雑ではありませんが、ExcelVBAの「次を検索」メソッドでのみ繰り返し発生するすべての検索が可能です。

この記事では、ExcelVBAでこの「次を検索」を使用する方法を紹介します。

Excel VBAで次を検索とは何ですか?

「次を検索」とは、見つかったセルから、検索を開始した元のセルに戻るまで、次の値を検索し続けることを意味します。

これは、指定された範囲内の指定された値を1回だけ検索する「検索」メソッドの高度なバージョンです。

以下は、ExcelVBAのFINDNEXTメソッドの構文です。

後:それは私たちが探している単語です。

ExcelVBAで次のメソッドを検索する例

以下は、ExcelVBAで次のメソッドを見つける例です。

たとえば、以下のデータを見てください。

このVBAFind Next Excelテンプレートはここからダウンロードできます– VBA Find Next Excel Template

ステップ1–このデータでは、都市名「バンガロール」を見つける必要があります。Visual BasicEditorでサブプロシージャを開始しましょう。

コード:

 Sub RangeNext_Example()End Sub 

ステップ2–まず、変数を「範囲」オブジェクトとして宣言します。

コード:

 Sub RangeNext_Example()Dim Rng As Range End Sub 

ステップ3–オブジェクト変数の参照を「Range( "A2:A11")」として設定します。

コード:

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

都市リストのデータは、この範囲のA2からA11までのセルの範囲にあるため、都市「バンガロール」のみを検索します。

範囲参照を変数「Rng」に設定するため、毎回RANGE(「A2:A11」)を使用する代わりにこの変数を使用します。

ステップ4– RNG変数を使用して、Findメソッドを開きます。

コード:

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

ステップ5– FINDメソッドの最初の引数は「What」、つまり上記の範囲で検索しようとしているものであるため、検索する値は「Bangalore」です。

コード:

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

ステップ6–この値が見つかったセルを示すために、もう1つの変数を文字列として宣言します。

コード:

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

ステップ7–この変数には、見つかったセルアドレスを割り当てます。

コード:

 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 

注: RNGは、見つかった値のセルの参照を持つため、RNG.Address。

ステップ8–割り当てられたセルアドレス変数の結果を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サブ 

ステップ9–コードを実行して、ここで何が得られるかを確認します。

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.