VBA RASTI TOLIAU - Kaip naudoti „FindNext“ funkciją „Excel VBA“?

„Excel VBA“ raskite toliau

Kaip ir „Excel“, kai paspaudžiame CTRL + F, pasirodo vedlio langelis, kuris leidžia mums ieškoti vertės pateiktame darbalapyje, o radę vertę, mes spustelėjame šalia, kad rastume kitą panašią vertę, nes tai yra darbalapio funkcija taip pat gali naudoti VBA kaip „Application“ nuosavybės metodą kaip „application.findnext“ tiems patiems tikslams.

Konkrečios vertės nustatymas minėtame diapazone yra puikus, bet ką daryti, jei reikalaujama rasti vertę su keliais atvejais. Viename iš ankstesnių straipsnių aptarėme „Rasti“ metodą VBA, ir jis visiškai nesudėtingas, tačiau surasti visus pasikartojančius įvykius įmanoma tik naudojant „Excel Next VBA“ metodą „Rasti kitą“.

Šiame straipsnyje mes parodysime, kaip naudoti šį „Find Next“ „Excel VBA“.

Kas yra „Excel VBA“ „Find Next“?

Kaip sako žodis, „Rasti kitą“ reiškia, kad iš rastos langelio toliau ieškokite kitos vertės, kol grįš į pradinę langelį, kuriame pradėjome paiešką.

Tai yra išplėstinė metodo „Rasti“ versija, ieškanti tik vieną kartą paminėtos vertės minėtame diapazone.

Žemiau yra „Excel VBA“ metodo RASTI KITĄ sintaksė.

Po: Tai yra žodis, kurio mes ieškome.

„Excel VBA“ kito metodo radimo pavyzdžiai

Toliau pateikiami kito „Excel VBA“ metodo paieškos pavyzdžiai.

Pavyzdžiui, pažiūrėkite į toliau pateiktus duomenis.

1 žingsnis - šiuose duomenyse turime rasti miesto pavadinimą „Bangalore“. Pradėkime antrinę procedūrą pagrindiniame vaizdiniame redaktoriuje.

Kodas:

Sub RangeNext_Example () Pabaigos sub

2 žingsnis. Pirmiausia paskelbkite kintamąjį kaip „Range“ objektą.

Kodas:

Sub RangeNext_Example () Dim Rng As Range End Sub

3 žingsnis - nustatykite objekto kintamojo nuorodą kaip „Range“ („A2: A11“).

Kodas:

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

Kadangi mūsų miesto sąrašo duomenys yra diapazone nuo A2 iki A11, tik mes ieškosime miesto „Bangalore“.

Kadangi mes nustatėme diapazono nuorodą į kintamąjį „Rng“, mes naudojame šį kintamąjį, užuot naudoję RANGE („A2: A11“) kiekvieną kartą.

4 žingsnis - naudokite kintamąjį RNG ir atidarykite „Find“ metodą.

Kodas:

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

5 žingsnis - Pirmasis FIND metodo argumentas yra „Ką“, ty, ko mes bandome ieškoti minėtame diapazone, taigi ieškoma vertė yra „Bangalore“.

Kodas:

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

6 žingsnis - norėdami parodyti, kurioje ląstelėje radome šią vertę, deklaruokite dar vieną kintamąjį kaip eilutę.

Kodas:

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

7 žingsnis - Šiam kintamajam priskirkite rastą langelio adresą.

Kodas:

Sub RangeNext_Example () Dim Rng As Range Dim CellAdderess as String Set Rng = Range ("A2: A12"). Raskite (kas: = "Bangalore") Rng.Find What: = "Bangalore" CellAddress = Rng.Address End Sub
Pastaba: RNG. Adresas, nes RNG turės rastos vertės langelio nuorodą.

8 žingsnis - Dabar VBA pranešimų laukelyje parodykite priskirtą langelio adreso kintamojo rezultatą.

Sub RangeNext_Example () Dim Rng As Range Dim CellAdderess as String Set Rng = Range ("A2: A12"). Raskite (kas: = "Bangalore") Rng.Find What: = "Bangalore" CellAddress = Rng.adress MsgBox CellAddress End Sub

9 žingsnis - paleiskite kodą ir sužinokite, ką mes čia gauname.

Taigi langelyje A5 radome vertę „Bangalore“. Taikydami „Find“ metodą galime rasti tik vieną langelį, todėl vietoje „FIND“ turime naudoti „FIND NEXT“ „Excel VBA“.

10 žingsnis - Turime nurodyti diapazono objekto kintamąjį, bet naudodami RASTI KITĄ metodą „Excel VBA“.

Kodas:

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) ciklas, kol „FirstCell“ FindRng.Address MsgBox „Paieška baigėsi“ Pabaiga Sub

21 žingsnis - tai ir toliau rodys visus atitinkančius langelių adresus, ir galų gale, jis pranešime parodys kaip „Paieška baigėsi“ naujame pranešimų laukelyje.

Ką reikia atsiminti

  • FIND metodas vienu metu gali rasti tik vieną vertę.
  • „Excel VBA“ RASTI KITĄ gali rasti kitą vertę iš jau rastos vertės langelio.
  • Naudokite kilpą „Do Nors“, norėdami peržvelgti visas langelio ribas.

Įdomios straipsniai...