Jan,
Je code is even snel als de mijne, het verschil ligt in het gebruik van de exacte ranges.
In werkelijkheid gaat het om tweemaal ongeveer 10.000 rijen.
Volgende instructie is zelfs sneller dan werken met variants:
rng.Offset(0, 2).Resize(1, 11).Value = shS.Range("A" & lngItem & ":K" & lngItem).Value
Jouw herwerkte code:
Sub Met_Dictionary()
Dim d As New Scripting.Dictionary
Dim rng As Range
Dim shS As Worksheet
Dim shD As Worksheet
Dim strKey As String
Dim lngItem As Long
'macrotimer
Application.ScreenUpdating = False
On Error GoTo Err_Met_Dictionary
Set shS = Worksheets("scangegevens")
Set shD = Worksheets("MEI")
shS.Activate
For Each rng In shS.Range(Cells(1, "A"), Cells(Application.Rows.Count, "A").End(xlUp))
strKey = rng & "#" & rng.Offset(0, 2)
If Not d.Exists(strKey) Then
d.Add strKey, rng.Row
End If
Next
shD.Activate
For Each rng In shD.Range(Cells(1, "E"), Cells(Application.Rows.Count, "E").End(xlUp))
strKey = rng & "#" & rng.Offset(0, -1)
If Len(d(strKey)) > 0 Then
lngItem = d(strKey)
rng.Offset(0, 2).Resize(1, 11).Value = shS.Range("A" & lngItem & ":K" & lngItem).Value
End If
Next
Exit_Met_Dictionary:
Set d = Nothing
Application.ScreenUpdating = True
'macrotimer
Exit Sub
Err_Met_Dictionary:
MsgBox Err.Number & vbNewLine & Err.Description
Resume Exit_Met_Dictionary
End Sub
Mijn herwerkte code:
Sub VerwerkingScangegevensBis()
Const BATCH As Integer = 5
Const PRODUCT As Integer = 1
Const GEWICHT As Integer = 3
Dim dicScanInput As Dictionary
Dim varScanInfo As Variant
Dim lngRow As Long
Dim strKey As String
Dim varSnijzaal As Variant
Dim varSplitter As Variant
Dim lngPos As Long
'macrotimer
Application.ScreenUpdating = False
varScanInfo = Sheets("scangegevens").Range("A1").CurrentRegion
Set dicScanInput = New Dictionary
With Sheets("scangegevens")
For lngRow = 1 To UBound(varScanInfo, 1)
strKey = varScanInfo(lngRow, BATCH) & " " & varScanInfo(lngRow, PRODUCT) & " " & varScanInfo(lngRow, GEWICHT)
If Not dicScanInput.Exists(strKey) Then dicScanInput.Add Key:=strKey, Item:=lngRow
Next lngRow
End With
varSnijzaal = ActiveSheet.Range(Cells(1, "A"), Cells(Application.Rows.Count, "E").End(xlUp))
Range("I4:S4") = Array("PRODUCT", "EENHEID", "GEWICHT", "VERPAKKING", "BATCH", "ORDERNR", "KLANTNR", "KLANT", "FACTUUR", "PRIJS/KG", "Totaal")
For lngRow = 5 To UBound(varSnijzaal, 1)
strKey = varSnijzaal(lngRow, 2) & " " & varSnijzaal(lngRow, 5) & " " & varSnijzaal(lngRow, 4)
If dicScanInput.Exists(strKey) Then
lngPos = dicScanInput(strKey)
Range(Cells(lngRow, "I"), Cells(lngRow, "S")) = Sheets("scangegevens").Range("A" & lngPos & ":K" & lngPos).Value
End If
Next lngRow
Set varScanInfo = Nothing
Set varSplitter = Nothing
Set varSnijzaal = Nothing
Set dicScanInput = Nothing
Application.ScreenUpdating = True
'macrotimer
End Sub
Macrotimer is een eigen routine (daarom in comment) die gebruik maakt van de hoge precisie timers (QueryPerformanceFrequency & QueryPerformanceCounter) . We zitten nu beiden rond de 650 msec. Jij een 20 msec sneller, dat komt waarschijnlijk doordat ik 3
criteria in rekening neem om de sleutel te maken.
Voor de volledigheid:
Private Declare PtrSafe Function QueryPerformanceCounter Lib "kernel32" (lpPerformanceCount As Currency) As LongPtr
Private Declare PtrSafe Function QueryPerformanceFrequency Lib "kernel32" (lpFrequency As Currency) As LongPtr
Public gcurFreq As Currency
Public gcurStart As Currency
Public gcurEnd As Currency
Public gcurOverhead As Currency
Public gcurAccumulatedTime As Currency
Public glngLoop As Long
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''' Procedure Name : MacroTimer
''' Procedure Type : Sub
''' Return Value :
''' Author © : Jean-Pierre Degroote
''' Date : 29/05/2016
''' Purpose :
'''---------------------------------------------------------------------------------------
Public Sub MacroTimer()
If gcurStart = 0 Then
If gcurFreq = 0 Then QueryPerformanceFrequency gcurFreq
If gcurFreq = 0 Then MsgBox prompt:="Your CPU does not support this timing method.", Title:=JPE_APP_TITLE
QueryPerformanceCounter gcurStart
Else
QueryPerformanceCounter gcurEnd
gcurAccumulatedTime = gcurAccumulatedTime + gcurEnd - gcurStart
glngLoop = glngLoop + 1
Debug.Print "Loop: "; Format(1000 * (gcurEnd - gcurStart) / gcurFreq, "0.000000"); " msec", "Avg: "; Format(1000 * gcurAccumulatedTime / (glngLoop * gcurFreq), "0.000000"); " msec", glngLoop
gcurStart = 0
End If
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''' Procedure Name : ResetTimer
''' Procedure Type : Sub
''' Return Value :
''' Author © : Jean-Pierre Degroote
''' Date : 29/05/2016
''' Purpose :
'''---------------------------------------------------------------------------------------
Public Sub ResetTimer()
gcurStart = 0
gcurEnd = 0
gcurFreq = 0
gcurAccumulatedTime = 0
glngLoop = 0
End Sub
Loop: 648,358105 msec Avg: 646,506811 msec 19
Loop: 648,524841 msec Avg: 646,607712 msec 20
Loop: 651,097396 msec Avg: 646,821507 msec 21
Loop: 660,823794 msec Avg: 647,457974 msec 22
Loop: 657,232648 msec Avg: 647,882960 msec 23
Loop: 651,017584 msec Avg: 648,013570 msec 24
Loop: 673,471234 msec Avg: 649,031876 msec 25
Loop: 682,109898 msec Avg: 650,304108 msec 26