Excel VBA Nájsť ďalšie
Rovnako ako v programe Excel, keď stlačíme kombináciu klávesov CTRL + F, objaví sa okno sprievodcu, ktoré nám umožní vyhľadať hodnotu v danom pracovnom hárku a akonáhle sa hodnota nájde, klikneme na nájsť vedľa a nájdeme ďalšiu podobnú hodnotu, pretože ide o funkciu pracovného hárka ho môžete na rovnaké účely použiť aj vo VBA ako metódu vlastnosti Application ako application.findnext.
Nájdenie konkrétnej hodnoty v uvedenom rozsahu je v poriadku, ale čo ak je požiadavkou vyhľadanie hodnoty s viacerými výskytmi. V jednom z predchádzajúcich článkov sme sa zaoberali metódou „Nájsť“ vo VBA a nie je to vôbec zložité, ale nájdenie všetkých opakujúcich sa výskytov je možné iba pomocou metódy „Nájsť ďalšie“ v programe Excel VBA.
V tomto článku si ukážeme, ako používať toto „Nájsť ďalšie“ v programe Excel VBA.
Čo je Nájsť ďalej v programe Excel VBA?
Ako hovorí slovo „Nájsť ďalšie“, znamená to, že od nájdenej bunky pokračujte v hľadaní ďalšej hodnoty, kým sa nevrátite späť do pôvodnej bunky, v ktorej sme zahájili vyhľadávanie.
Toto je pokročilá verzia metódy „Nájsť“, ktorá vyhľadáva iba jednu uvedenú hodnotu v uvedenom rozsahu.
Nižšie je uvedená syntax metódy FIND NEXT v programe Excel VBA.
Potom: Je to slovo, ktoré hľadáme.
Príklady metódy Nájsť ďalšiu v programe Excel VBA
Nižšie uvádzame príklady metódy nájdenia ďalšej v programe Excel VBA.
Napríklad si pozrite nasledujúce údaje.
Túto šablónu VBA Find Next Excel si môžete stiahnuť tu - VBA Find Next Excel TemplateKrok 1 - V týchto údajoch musíme nájsť názov mesta „Bangalore“. Začnime podproces v editore jazyka Visual Basic.
Kód:
Sub RangeNext_Example () End Sub
Krok 2 - Najskôr deklarujte premennú ako objekt „Range“.
Kód:
Sub RangeNext_Example () Dim Rng ako Range End Sub
Krok 3 - Nastavte odkaz na premennú objektu ako „Rozsah („ A2: A11 “).
Kód:
Sub RangeNext_Example () Dim Rng As Range Set Rng = Range ("A2: A12") End Sub
Pretože naše údaje zo zoznamu miest sú v rozsahu buniek od A2 do A11 v tomto rozsahu, budeme hľadať iba mesto „Bangalore“.
Pretože sme nastavili odkaz na rozsah pre premennú „Rng“, použijeme túto premennú namiesto toho, aby sme zakaždým používali RANGE („A2: A11“).
Krok 4 - Použite premennú RNG a otvorte metódu Nájsť.
Kód:
Sub RangeNext_Example () Dim Rng As Set Set Rng = Range ("A2: A12") Rng.Find End Sub
Krok č. 5 - Prvý argument metódy FIND je „Čo“, tj. Čo sa snažíme vyhľadávať v uvedenom rozsahu, takže hľadaná hodnota je „Bangalore“.
Kód:
Sub RangeNext_Example () Dim Rng As Range Set Rng = Range ("A2: A12") Rng.Find What: = "Bangalore" End Sub
Krok č. 6 - Ak chcete ukázať, v ktorej bunke sme našli túto hodnotu, deklarujte ako premennú ešte jednu premennú.
Kód:
Sub RangeNext_Example () Dim Rng As Range Dim CellAdderess As String Set Rng = Range ("A2: A12") Rng.Find What: = "Bangalore" End Sub
Krok č. 7 - K tejto premennej priraďte nájdenú adresu bunky.
Kód:
Sub RangeNext_Example () Dim Rng As Range Dim CellAdderess As String Set Rng = Range ("A2: A12"). Nájsť (What: = "Bangalore") Rng.Find What: = "Bangalore" CellAddress = Rng.Address End SubPoznámka: RNG.Address, pretože RNG bude mať referenciu pre bunku nájdenej hodnoty.
Krok 8 - Teraz ukážte výsledok priradenej premennej adresy bunky v okne správy vo VBA.
Sub RangeNext_Example () Dim Rng As Range Dim CellAdderess As String Set Rng = Range ("A2: A12"). Nájsť (What: = "Bangalore") Rng.Find What: = "Bangalore" CellAddress = Rng.Address MsgBox CellAddress End Sub
Krok 9 - Spustite kód a uvidíte, čo tu dostaneme.
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.