VBA 다음 찾기 | Excel VBA에서 FindNext 함수를 사용하는 방법?

Excel VBA 다음 찾기

Excel에서와 같이 CTRL + F를 누르면 마법사 상자가 나타나 주어진 워크 시트에서 값을 검색 할 수 있고 값이 발견되면 다음 찾기를 클릭하여 워크 시트 기능이므로 다른 유사한 값을 찾습니다. 같은 목적으로 VBA에서 application.findnext와 같은 Application 속성 메서드로 사용할 수도 있습니다.

언급 된 범위에서 특정 값을 찾는 것은 좋지만 요구 사항이 여러 번 발생하는 값을 찾는 것이라면 어떻게 될까요? 이전 기사 중 하나에서 VBA의 "찾기"방법에 대해 논의했으며 전혀 복잡하지 않지만 Excel VBA의 "다음 찾기"방법을 통해서만 반복되는 모든 항목을 찾을 수 있습니다.

이 기사에서는 Excel VBA에서이 "다음 찾기"를 사용하는 방법을 보여줍니다.

Excel VBA에서 다음 찾기 란 무엇입니까?

"다음 찾기"라는 단어는 찾은 셀에서 검색을 시작한 원래 셀로 돌아갈 때까지 다음 값을 계속 검색한다는 의미입니다.

이것은 언급 된 범위에서 언급 된 값을 한 번만 검색하는 "찾기"방법의 고급 버전입니다.

다음은 Excel VBA의 FIND NEXT 메서드 구문입니다.

이후 : 우리가 찾고있는 단어입니다.

Excel VBA에서 다음 찾기 방법의 예

다음은 Excel VBA에서 다음 찾기 방법의 예입니다.

예를 들어, 아래 데이터를보십시오.

이 VBA 다음 Excel 템플릿 찾기-VBA 다음 Excel 템플릿 찾기

1 단계 – 이 데이터에서 "Bangalore"라는 도시 이름을 찾아야합니다. Visual Basic 편집기에서 하위 절차를 시작하겠습니다.

암호:

 하위 범위 Next_Example () End Sub 

2 단계 – 먼저 변수를 "Range"개체로 선언합니다.

암호:

 하위 범위 Next_Example () Dim Rng As Range End Sub 

3 단계 – 개체 변수에 대한 참조를 "Range ("A2 : A11 ")로 설정합니다.

암호:

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

도시 목록의 데이터가이 범위의 A2에서 A11까지의 셀 범위에 있기 때문에 "방갈로르"도시를 검색 할 것입니다.

범위 참조를 변수 "Rng"로 설정 했으므로 매번 RANGE ( "A2 : A11")를 사용하는 대신이 변수를 사용합니다.

단계 # 4 – RNG 변수를 사용하고 Find 메서드를 엽니 다.

암호:

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

Step # 5 – FIND 메서드의 첫 번째 인수는 "What"입니다. 즉 언급 된 범위에서 검색하려는 항목이므로 검색하는 값은 "Bangalore"입니다.

암호:

 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 범위 Dim CellAdderess As String Set Rng = Range ( "A2 : A12") Rng.Find What : = "Bangalore"End Sub 

Step # 7 – 이 변수에 대해 찾은 셀 주소를 할당합니다.

암호:

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

참고 : RNG는 찾은 값 셀에 대한 참조를 가지므로 RNG.Address입니다.

단계 # 8 – 이제 VBA의 메시지 상자에 할당 된 셀 주소 변수 결과를 표시합니다.

 Sub RangeNext_Example () Dim Rng As 범위 Dim CellAdderess As String Set Rng = Range ( "A2 : A12"). Find (What : = "Bangalore") Rng.Find What : = "Bangalore"CellAddress = Rng.Address MsgBox CellAddress End 보결 

Step # 9 – 코드를 실행하고 여기에 무엇이 있는지 확인하십시오.

그래서 우리는 A5 셀에서“Bangalore”값을 찾았습니다. Find 메서드를 사용하면 하나의 셀만 찾을 수 있으므로 FIND 대신 Excel VBA에서 FIND NEXT를 사용해야합니다.

단계 # 10 – 범위 개체 변수를 참조해야하지만 Excel VBA에서 FIND NEXT 메서드를 사용합니다.

암호:

 Sub RangeNext_Example () Dim Rng As 범위 Dim CellAdderess As String Set Rng = Range ( "A2 : A12"). Find (What : = "Bangalore") Rng.Find What : = "Bangalore"CellAddress = Rng.Address MsgBox CellAddress 세트 Rng = Range ( "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.