VBA ค้นหาถัดไป | วิธีใช้ฟังก์ชัน FindNext ใน Excel VBA

Excel VBA ค้นหาถัดไป

เช่นเดียวกับใน excel เมื่อเรากด CTRL + F กล่องตัวช่วยจะปรากฏขึ้นซึ่งช่วยให้เราสามารถค้นหาค่าในแผ่นงานที่กำหนดและเมื่อพบค่าแล้วให้คลิกค้นหาถัดไปเพื่อค้นหาค่าอื่นที่คล้ายกันเนื่องจากเป็นคุณสมบัติของแผ่นงานที่เรา ยังสามารถใช้ใน VBA เป็นวิธีการคุณสมบัติของแอปพลิเคชันเป็น application.findnext เพื่อวัตถุประสงค์เดียวกัน

การค้นหาค่าเฉพาะในช่วงที่กล่าวถึงนั้นใช้ได้ แต่จะเกิดอะไรขึ้นถ้าความต้องการคือการค้นหาค่าที่มีหลายเหตุการณ์ ในบทความก่อนหน้านี้เราได้พูดถึงวิธีการ "ค้นหา" ใน VBA แล้วและมันไม่ซับซ้อนเลย แต่การค้นหาเหตุการณ์ที่เกิดขึ้นซ้ำ ๆ ทั้งหมดนั้นเป็นไปได้ด้วยวิธี "ค้นหาถัดไป" ใน excel VBA เท่านั้น

ในบทความนี้เราจะแสดงวิธีใช้ "ค้นหาถัดไป" ใน Excel VBA

Find Next ใน Excel VBA คืออะไร

ดังที่คำว่า "ค้นหาถัดไป" หมายถึงจากเซลล์ที่พบให้ค้นหาค่าถัดไปจนกว่าจะกลับไปที่เซลล์เดิมที่เราได้เริ่มการค้นหา

นี่คือวิธีการ "ค้นหา" เวอร์ชันขั้นสูงซึ่งจะค้นหาค่าที่กล่าวถึงเพียงครั้งเดียวในช่วงที่กล่าวถึง

ด้านล่างนี้คือไวยากรณ์ของวิธี FIND NEXT ใน Excel VBA

After:มันคือคำที่เรากำลังค้นหา

ตัวอย่างของ Find Next Method ใน Excel VBA

ด้านล่างนี้เป็นตัวอย่างของวิธีการค้นหาถัดไปใน excel VBA

ตัวอย่างเช่นดูข้อมูลด้านล่าง

คุณสามารถดาวน์โหลด VBA Find Next Excel Template ได้ที่นี่ - VBA Find Next Excel Template

ขั้นตอนที่ # 1 -ในข้อมูลนี้เราต้องหาชื่อเมือง "บังกาลอร์" มาเริ่มขั้นตอนย่อยในโปรแกรมแก้ไขภาพพื้นฐาน

รหัส:

 Sub RangeNext_Example () End Sub 

ขั้นตอน # 2 -ขั้นแรกประกาศตัวแปรเป็นวัตถุ "ช่วง"

รหัส:

 Sub RangeNext_Example () Dim Rng เป็น Range End Sub 

ขั้นตอนที่ # 3 -ตั้งค่าการอ้างอิงสำหรับตัวแปรออบเจ็กต์เป็น "ช่วง (" A2: A11 ")

รหัส:

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

เนื่องจากข้อมูลรายชื่อเมืองของเราอยู่ในช่วงเซลล์ตั้งแต่ A2 ถึง A11 ในช่วงนี้เราจึงจะค้นหาเมือง“ บังกาลอร์”

เนื่องจากเราตั้งค่าการอ้างอิงช่วงเป็นตัวแปร“ Rng” เราจึงใช้ตัวแปรนี้แทนการใช้ RANGE (“ A2: A11”) ทุกครั้ง

ขั้นตอน # 4 -ใช้ตัวแปร RNG และเปิดวิธีการค้นหา

รหัส:

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

ขั้นตอนที่ # 5 -อาร์กิวเมนต์แรกของวิธี FIND คือ“ อะไร” นั่นคือสิ่งที่เราพยายามค้นหาในช่วงที่กล่าวถึงดังนั้นค่าที่เรากำลังค้นหาคือ“ บังกาลอร์”

รหัส:

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

ขั้นตอนที่ # 6 -เพื่อแสดงว่าเซลล์ใดที่เราพบค่านี้ให้ประกาศอีกหนึ่งตัวแปรเป็นสตริง

รหัส:

 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") ค้นหา (What: = "Bangalore") Rng.Find What: = "Bangalore" CellAddress = Rng.Address End Sub 

หมายเหตุ: RNG.Address เนื่องจาก RNG จะมีการอ้างอิงสำหรับเซลล์ค่าที่พบ

ขั้นตอน # 8 -ตอนนี้แสดงผลตัวแปรที่อยู่เซลล์ที่กำหนดในกล่องข้อความใน VBA

 Sub RangeNext_Example () Dim Rng As Range Dim CellAdderess As String Set Rng = Range ("A2: A12") ค้นหา (What: = "Bangalore") Rng.Find What: = "Bangalore" CellAddress = Rng.Address MsgBox CellAddress End ย่อย 

ขั้นตอนที่ # 9 -เรียกใช้รหัสและดูว่าเราได้อะไรที่นี่

เราจึงพบค่า“ บังกาลอร์” ในเซลล์ A5 ด้วยวิธีการค้นหาเราสามารถค้นหาได้เพียงเซลล์เดียวดังนั้นแทนที่จะเป็น FIND เราต้องใช้ FIND NEXT ใน excel VBA

ขั้นตอนที่ # 10 -เราจำเป็นต้องอ้างอิงตัวแปร range object แต่ใช้วิธี FIND NEXT ใน excel VBA

รหัส:

 Sub RangeNext_Example () Dim Rng As Range Dim CellAdderess As String Set Rng = Range ("A2: A12") ค้นหา (What: = "Bangalore") Rng.Find What: = "Bangalore" CellAddress = Rng.Address MsgBox CellAddress Set Rng = ช่วง ("A2: A12") FindNext (Rng) End Sub 

ดังที่คุณเห็นด้านบนเราได้ใช้วิธี VBA FIND NEXT แต่ภายในฟังก์ชันเราได้ใช้ชื่อตัวแปรออบเจ็กต์ช่วง

ขั้นตอนที่ # 11 -กำหนดที่อยู่เซลล์อีกครั้งและแสดงที่อยู่ในกล่องข้อความ

รหัส:

 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.