Excel에서 모든 워크 시트에서 특정 함수의 모든 인스턴스를 찾는 VBA 매크로를 만들었습니다. 나는 성공적으로 그것을 만들 수 있었지만, 내가 찾고있는 함수를 현명하게 수행하는 가장 좋은 방법이 무엇인지 알아 내려고하고있다. 큰 통합 문서로 많은 시간을 잠재적으로 사용할 수있다.VBA Find 메서드를 사용하여 셀을 빠르게 검색하거나 모든 셀을 검색합니까?
두 가지 방법을 사용했습니다.
방법 1 - 각 개별 셀을 반복하고 "instr"함수를 사용하여 셀 수식에 해당 함수가 들어 있는지 확인하십시오.
방법 2 - Find 루프와 함께 Find 및 FindNext 메서드를 사용하여 실제로 함수가있는 셀을 순환합니다.
많은 수의 함수가있을 때 놀랐습니다. 방법 1은 훨씬 빠릅니다 (메서드 2가 매우 빠르게 작동하는 경우).
누구나 그 방법을 설명 할 수 있습니까?
다음은 예제 코드 예제입니다.
"Sheet1"에서 "MyFunction"이라는 사용자 정의 함수를 A1 : J5000 셀에 배치했습니다. 그런 다음 셀 A5001 : J10000에서 비워 뒀지 만 사용 된 범위가 A1 : J10000이되도록 황색으로 표시했습니다.
는 방법 1이 평균 실행이 약 1531 인이 훨씬 빨리 50,000 세포가방법 1의 평균 실행이 약 171 MS 및 방법입니다 발견을 통해서만 루프 방법이보다 10 만 개 세포를 통해 반복하더라도 ms. 방법 1과 방법 2에 대한 내 코드의
예 :
방법 1
Private Sub TestMethod1()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Dim MySheet As Worksheet, MyRange As Range, MyCell As Range
Dim MyCellAddress As String, MyCellFormula As String, MyFunction As String
Dim CountTotalCells As Long, CountTotalFunctions As Long
Dim sw, swEndTime As Long
Set sw = New StopWatch
sw.StartTimer
MyFunction = "=MyFunction("
CountTotalCells = 0
CountTotalFunctions = 0
Set MySheet = Sheets("Forum Question")
Set MyRange = MySheet.UsedRange
For Each MyCell In MyRange
MyCellFormula = MyCell.Formula
CountTotalCells = CountTotalCells + 1
If InStr(1, MyCellFormula, MyFunction) > 0 Then
CountTotalFunctions = CountTotalFunctions + 1
End If
Next
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
swEndTime = sw.EndTimer
MsgBox CountTotalCells & ", " & CountTotalFunctions & ", " & swEndTime & " ms"
End Sub
방법은 2
Private Sub TestMethod2()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Dim MySheet As Worksheet, MyRange As Range, MyCell As Range
Dim MyCellAddress As String, MyCellFormula As String, MyFunction As String, MyCellFirst As String
Dim CountTotalCells As Long, CountTotalFunctions As Long
Dim sw, swEndTime As Long
Set sw = New StopWatch
sw.StartTimer
MyFunction = "=MyFunction("
CountTotalCells = 0
CountTotalFunctions = 0
Set MySheet = Sheets("Forum Question")
Set MyRange = MySheet.UsedRange
Set MyCell = MyRange.Cells.Find(_
What:=MyFunction, _
After:=[A1], _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlRows, _
SearchDirection:=xlNext, _
MatchCase:=True _
)
If Not MyCell Is Nothing Then
MyCellFirst = MyCell.Address
Do
Set MyCell = MyRange.FindNext(After:=MyCell)
MyCellAddress = MyCell.Address
MyCellFormula = "z" & MyCell.Formula
CountTotalCells = CountTotalCells + 1
If InStr(1, MyCellFormula, MyFunction) > 0 Then
CountTotalFunctions = CountTotalFunctions + 1
End If
If MyCell Is Nothing Or MyCellAddress = MyCellFirst Then
Exit Do
End If
Loop
End If
Set MyCell = Nothing
swEndTime = sw.EndTimer
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
MsgBox CountTotalCells & ", " & CountTotalFunctions & ", " & swEndTime & " ms"
End Sub
이 게시물은 [StackExchange] (http://codereview.stackexchange.com/)에 더 적합하지 않습니까? – user3598756