VBA מצא הבא | כיצד להשתמש בפונקציית FindNext ב- Excel VBA?

Excel VBA מצא הבא

כמו ב- Excel כאשר אנו לוחצים על CTRL + F קופצת תיבת אשפים המאפשרת לנו לחפש ערך בגליון העבודה הנתון וברגע שהערך נמצא אנו לוחצים על find לצד למצוא את הערך הדומה האחר, מכיוון שהוא תכונה של גליון עבודה אנו יכול להשתמש בו גם ב- VBA כשיטת מאפיין יישום כיישום.findnext לאותן מטרות.

מציאת הערך הספציפי בטווח שהוזכר זה בסדר אבל מה אם הדרישה היא למצוא את הערך עם מספר מופעים. באחד המאמרים הקודמים, דנו בשיטת "מצא" ב- VBA והיא אינה מורכבת כלל, אך מציאת כל ההתרחשויות החוזרות אפשרית רק בשיטת "מצא הבא" ב- VBA מצטיין.

במאמר זה אנו נראה לך כיצד להשתמש ב"מצא הבא "ב- Excel VBA.

מה נמצא הבא ב- Excel VBA?

כמו שאומרת המילה "מצא הבא" פירושו שהתא שנמצא ממשיך לחפש את הערך הבא עד שהוא חוזר לתא המקורי בו התחלנו את החיפוש.

זו הגרסה המתקדמת של שיטת "מצא" המחפשת פעם אחת בלבד בערך המוזכר בטווח המוזכר.

להלן התחביר של שיטת FIND NEXT ב- VBA של Excel.

אחרי: זו המילה שאנחנו מחפשים.

דוגמאות למציאת השיטה הבאה ב- Excel VBA

להלן דוגמאות למציאת השיטה הבאה ב- VBA מצטיינת.

לדוגמה, עיין בנתונים הבאים.

אתה יכול להוריד את תבנית ה- VBA הבאה של Excel כאן - VBA מצא את תבנית ה- Excel הבאה

שלב 1 - בנתונים אלה עלינו למצוא את שם העיר "בנגלור". נתחיל בתהליך המשנה בעורך הבסיסי הויזואלי.

קוד:

 Sub RangeNext_Example () Sub Sub 

שלב 2 - ראשית, הכריז על המשתנה כאובייקט "טווח".

קוד:

 Sub RangeNext_Example () עמעום Rng כ- Sub Range End Sub 

שלב 3 - הגדר את ההפניה למשתנה האובייקט כ- "טווח (" A2: A11 ").

קוד:

 Sub RangeNext_Example () עמעום Rng כטווח הגדר Rng = טווח ("A2: A12") סוף משנה 

מכיוון שהנתונים שלנו על רשימת הערים נמצאים בטווח התאים בין A2 ל- A11 בטווח זה בלבד אנו הולכים לחפש את העיר "בנגלור".

מכיוון שקבענו את הפניה לטווח למשתנה "Rng" אנו משתמשים במשתנה זה במקום להשתמש ב- RANGE ("A2: A11") בכל פעם.

שלב 4 - השתמש במשתנה RNG ופתח את שיטת החיפוש.

קוד:

 תת טווח הבא_דוגמה () עמעום Rng כטווח הגדר Rng = טווח ("A2: A12") Rng. מצא סוף משנה 

שלב 5 - הטיעון הראשון של שיטת FIND הוא "מה" כלומר מה שאנחנו מנסים לחפש בטווח שהוזכר, ולכן הערך שאנו מחפשים הוא "בנגלור".

קוד:

 Sub RangeNext_Example () עמעום Rng כטווח הגדר Rng = טווח ("A2: A12") Rng. מצא מה: = "בנגלור" משנה משנה 

שלב 6 - כדי להציג באיזה תא מצאנו ערך זה הכריז על משתנה אחד נוסף כמחרוזת.

קוד:

 תת טווח הבא_דוגמה () עמעום Rng כטווח אפלולית של תאים כמו מחרוזת הגדר Rng = טווח ("A2: A12") Rng. מצא מה: = "בנגלור" סוף משנה 

שלב 7 - עבור משתנה זה הקצה את כתובת התא שנמצאה.

קוד:

 תת RangeNext_Example () עמעום Rng כטווח אפלולית של תאים כמו מחרוזת הגדר Rng = טווח ("A2: A12"). מצא (מה: = "בנגלור") Rng. מצא מה: = "בנגלור" CellAddress = Rng. כתובת סוף משנה 

הערה: RNG. כתובת מכיוון ש- RNG יקבל את ההפניה לתא הערך שנמצא.

שלב 8 - כעת הראה את התוצאה המשתנה של כתובת התא המוקצה בתיבת ההודעות ב- VBA.

 תת טווח הבא_דוגמה () עמעום Rng כטווח אפלולית של תאים כמו מחרוזת הגדר Rng = טווח ("A2: A12"). מצא (מה: = "בנגלור") Rng. מצא מה: = "בנגלור" CellAddress = Rng. כתובת 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.