Excel VBA Sonrakini Bul
Excel'de olduğu gibi, CTRL + F'ye bastığımızda, verilen çalışma sayfasında bir değeri aramamıza izin veren bir sihirbaz kutusu açılır ve değer bulunduğunda, diğer benzer değeri bulmak için yanındaki bul'a tıklarız, çünkü bu bir çalışma sayfası özelliği olduğu için aynı amaçlar için application.findnext olarak VBA'da Application özelliği yöntemi olarak da kullanabilir.
Belirtilen aralıkta belirli bir değeri bulmak iyidir, ancak şart, değeri birden çok kez bulmaksa ne olur? Daha önceki makalelerden birinde, VBA'daki "Bul" yöntemini tartıştık ve bu hiç karmaşık değil, ancak tüm tekrar eden olayları bulmak yalnızca excel VBA'daki "Sonrakini Bul" yöntemiyle mümkündür.
Bu makalede, Excel VBA'da bu "Sonrakini Bul" u nasıl kullanacağınızı göstereceğiz.
Excel VBA'da Sonrakini Bul nedir?
Kelimenin dediği gibi "Sonrakini Bul", bulunan hücreden, aramaya başladığımız orijinal hücreye dönene kadar sonraki değeri aramaya devam et anlamına gelir.
Bu, belirtilen aralıkta belirtilen değeri yalnızca bir kez arayan "Bul" yönteminin gelişmiş sürümüdür.
Excel VBA'da SONRAKİ BUL yönteminin sözdizimi aşağıdadır.
Sonra: Aradığımız kelime.
Excel VBA'da Sonraki Bul Yöntemine Örnekler
Aşağıda, excel VBA'da sonraki yöntemi bulma örnekleri verilmiştir.
Örneğin, aşağıdaki verilere bakın.
Bu VBA'yı Bul Sonraki Excel Şablonunu buradan indirebilirsiniz - VBA Sonrakini Bul Excel ŞablonuAdım # 1 - Bu verilerde "Bangalore" şehir adını bulmamız gerekiyor. Görsel temel düzenleyicide alt prosedüre başlayalım.
Kod:
Alt AralıkNext_Example () End Sub
Adım # 2 - İlk önce değişkeni "Aralık" nesnesi olarak bildirin.
Kod:
Alt AralıkNext_Example () Aralık Sonu Alt Olarak Dim Aralığı
Adım # 3 - Nesne değişkeni için referansı "Aralık (" A2: A11 ") olarak ayarlayın.
Kod:
Alt AralıkNext_Example () Dim Aralığı Aralık Olarak Ayarla Aralığı = Aralık ("A2: A12") Son Alt
Şehir listesi verilerimiz bu aralıkta A2'den A11'e kadar olan hücrelerde bulunduğundan, sadece “Bangalore” şehrini arayacağız.
Aralık referansını "Rng" değişkenine ayarladığımız için, her seferinde RANGE ("A2: A11") kullanmak yerine bu değişkeni kullanırız.
Adım # 4 - RNG değişkenini kullanın ve Bul yöntemini açın.
Kod:
Alt AralıkNext_Example () Dim Aralığı Aralık Olarak Ayarla Aralığı = Aralık ("A2: A12") Rng.Find End Sub
Adım # 5 - FIND yönteminin ilk argümanı "Ne" dir, yani bahsedilen aralıkta aramaya çalıştığımız şey, yani aradığımız değer "Bangalore" dir.
Kod:
Alt AralıkNext_Example () Dim Aralığı Aralık Olarak Ayarla Aralığı = Aralık ("A2: A12") Rng.Ne Bul: = "Bangalore" End Sub
Adım # 6 - Bu değeri hangi hücrede bulduğumuzu göstermek için bir değişken daha dize olarak bildirin.
Kod:
Alt AralıkNext_Example () Dim Rng As Range Dim CellAdderess As String Set Rng = Range ("A2: A12") Rng.Ne Bul: = "Bangalore" End Sub
Adım # 7 - Bu değişken için bulunan hücre adresini atayın.
Kod:
Sub RangeNext_Example () Dim Rng As Range Dim CellAdderess As String Set Rng = Range ("A2: A12"). Find (What: = "Bangalore") Rng.Ne Bul: = "Bangalore" CellAddress = Rng.Address End SubNot: RNG.Address çünkü RNG, bulunan değer hücresi için referansa sahip olacaktır.
Adım # 8 - Şimdi, atanan hücre adresi değişkeni sonucunu VBA'daki mesaj kutusunda gösterin.
Sub RangeNext_Example () Dim Rng As Range Dim CellAdderess As String Set Rng = Range ("A2: A12"). Find (What: = "Bangalore") Rng.Neyi Bul: = "Bangalore" CellAddress = Rng.Address MsgBox CellAddress End Alt
Adım # 9 - Kodu çalıştırın ve burada ne alacağımızı görün.
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.