I have a problem with filtering and adding data to collections in my Excel VBA code.
I have two workbooks: SourceWB and SourceTR. I gather data from both and list them in the SourceTR. The goal is to compare both data sets and find mismatches. The code is run when the SourceTR is active.
I've omitted the rest of the code, here is just the problematic part:
Debug.Print "3 -- " & Now
For Each i In Workbooks("SourceTR").Worksheets("Source1").Range("A4:A10000")
    If i.Value <> "" Then
        If month(i.Value) = selected_month Then
            item_1 = Worksheets("Source1").Range("E" & i.row).Value
            item_2 = Worksheets("Source1").Range("F" & i.row).Value
            item_3 = Worksheets("Source1").Range("K" & i.row).Value
            entry = item_1 & "_" & item_2 & "_" & item_3
            If IsInCollection(init_tr_entries, entry) = False Then
                init_tr_entries.Add (entry)
            End If
        End If
    End If
Next i
Debug.Print "4 -- " & Now
Dim coll_item
For Each coll_item In init_tr_entries
    Workbooks("SourceTR").Worksheets("target").Range("A" & starting_row_1).Value = Split(coll_item, "_")(0)
    Workbooks("SourceTR").Worksheets("target").Range("B" & starting_row_1).Value = Split(coll_item, "_")(1)
    Workbooks("SourceTR").Worksheets("target").Range("C" & starting_row_1).Value = Split(coll_item, "_")(2)
    starting_row_1 = starting_row_1 + 1
Next coll_item
Debug.Print "5 -- " & Now
Dim a As Range
Dim user As String
user = Worksheets("vir").Range("G2").Value
Dim init_as_entries As New Collection
For Each a In Workbooks("SourceWB"))).Worksheets("Source2")).Range("BU4:BU10000")
    If a.Value <> "" Then
        If a.Value = "" & selected_month & "" Then
            If Workbooks("SourceWB"))).Worksheets("Source2")).Range("F" & a.row).Value = user Then
                item_1 = Workbooks("SourceWB"))).Worksheets("Source2")).Range("A" & a.row).Value
                item_2 = Workbooks("SourceWB"))).Worksheets("Source2")).Range("B" & a.row).Value
                item_3 = Workbooks("SourceWB"))).Worksheets("Source2")).Range("E" & a.row).Value
                entry = item_1 & "_" & item_2 & "_" & item_3
                init_as_entries.Add (entry)
            End If
        End If
    End If
Next a
For Each coll_item In init_as_entries
    Workbooks("SourceTR").Worksheets("target").Range("F" & starting_row_2).Value = Split(coll_item, "_")(0)
    Workbooks("SourceTR").Worksheets("target").Range("G" & starting_row_2).Value = Split(coll_item, "_")(1)
    Workbooks("SourceTR").Worksheets("target").Range("H" & starting_row_2).Value = Split(coll_item, "_")(2)
    starting_row_2 = starting_row_2 + 1
Next coll_item
Debug.Print "6 -- " & Now
The code between point 3 and 5 takes about 1 second and the code between 5 and 6 takes about 10 seconds. However, other that some filtering I don't see any difference in the code.
The data sets are small, 2500 non blank rows in SourceWB and only 60 in SourceTR.
What am I doing wrong?
---EDIT--- I've done some additional measurements and this part:
For Each a In Workbooks("SourceWB"))).Worksheets("Source2")).Range("BU4:BU10000")
    If a.Value <> "" Then
        If a.Value = "" & selected_month & "" Then
            If Workbooks("SourceWB"))).Worksheets("Source2")).Range("F" & a.row).Value = user Then
                item_1 = Workbooks("SourceWB"))).Worksheets("Source2")).Range("A" & a.row).Value
                item_2 = Workbooks("SourceWB"))).Worksheets("Source2")).Range("B" & a.row).Value
                item_3 = Workbooks("SourceWB"))).Worksheets("Source2")).Range("E" & a.row).Value
                entry = item_1 & "_" & item_2 & "_" & item_3
                init_as_entries.Add (entry)
            End If
        End If
    End If
Next a
takes 7 seconds with this speed enhancements:
Application.ScreenUpdating = False
Application.Calculation = xlManual
Application.EnableEvents = False
Application.DisplayStatusBar = False
ActiveSheet.DisplayPageBreaks = False
 
     
    