Is there anything especially memory-inefficient for the below code snippet? It is making Excel seem to run forever for large values (>50,000) of Long2
. It works fine for medium and small values of Long2, but for larger values, it does not run a proportionally longer time, it seems to never finish. Pushing Ctrl-Break gives only the spinning progress wheel and Not Responding, even though I have DoEvents
earlier in my code to display a progress bar.
Am I reading off the worksheet over and over? It is my impression that I am reading off the Range
objects in memory, but please correct me if I am wrong.
Please excuse my Hungarian notation.
''A' and 'AV' Matches
If boolMatchesSheetExists = True Then
With ThisWorkbook.Sheets("Matches")
Long2 = Application.CountA(.Columns(1))
Set Range2 = .Range(.Cells(1, 1), .Cells(Long2, 1))
Set Range3 = .Range(.Cells(1, 19), .Cells(Long2, 19))
Set Range4 = .Range(.Cells(1, 36), .Cells(Long2, 36))
End With
For Each Cell1 In Range1
''A' Matches
If Application.CountIfs(Range2, Cell1.Value, Range3, "<>" & Cell1.Value, Range4, "A") > 0 Then
.Cells(Cell1.Row, 2).Value = 1
Else
.Cells(Cell1.Row, 2).Value = 0
End If
''AV' Matches
If Application.CountIfs(Range2, Cell1.Value, Range3, "<>" & Cell1.Value, Range4, "AV") > 0 Then
.Cells(Cell1.Row, 3).Value = 1
Else
.Cells(Cell1.Row, 3).Value = 0
End If
''A' Claims Duping Against Self
If Application.CountIfs(Range2, Cell1.Value, Range3, Cell1.Value) > 0 Then
.Cells(Cell1.Row, 4).Value = 1
Else
.Cells(Cell1.Row, 4).Value = 0
End If
Next Cell1
End If
Edit: Per @200_success 's suggestion, this code populates a summary sheet ("Summary") with boolean results (0, 1) for whether matches of particular types ('A', 'AV', 'Self-Dup') were found. If you look closely at the above code you'll notice there is a missing With
block that should contain the whole thing - in reality, there is one: With ThisWorkbook.Sheets("Summary")
.