1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| Option Explicit
Public Const rowStart = 500
Public Const rowEnd = 10500
Public Const colSourceA = 1
Public Const colSourceB = colSourceA + 1
Public Const colResult = colSourceB + 1
Sub MesurePerf()
Dim tStart As Double, tEnd As Double
ClearResult
tStart = Time
epureFOREACHB1_Matt
tEnd = Time
Debug.Print "Matt: " + Format(tEnd - tStart, "HH:MM:SS")
ClearResult ' Mettre le point d'arrêt dans la marge de cette ligne
tStart = Time
epureFOREACHB1_JP
tEnd = Time
Debug.Print "JP: " + Format(tEnd - tStart, "HH:MM:SS")
End Sub
Sub epureFOREACHB1_Matt()
Dim indRowSource As Integer, indRowTarget As Integer, value As Integer
Dim collResult As Collection, collB As Collection, strKey As String
Application.ScreenUpdating = False: Application.Calculation = xlCalculationManual
Set collB = New Collection: Set collResult = New Collection
For indRowSource = rowStart To rowEnd
strKey = CStr(Cells(indRowSource, colSourceB))
If Not IsInCollection(collB, strKey) Then
collB.Add vbNull, key:=strKey ' The value is not used in the collection only the key
End If
Next
indRowTarget = rowStart
For indRowSource = rowStart To rowEnd
value = Cells(indRowSource, colSourceA)
strKey = CStr(value)
If Not IsInCollection(collB, strKey) Then
If Not IsInCollection(collResult, strKey) Then
collResult.Add vbNull, key:=strKey
Cells(indRowTarget, colResult) = value
indRowTarget = indRowTarget + 1
End If
End If
Next
Application.ScreenUpdating = True: Application.Calculation = xlCalculationAutomatic
Set collResult = Nothing: Set collB = Nothing
End Sub
Function IsInCollection(ByVal collList As Collection, ByVal strKey As String) As Boolean
Dim value As Integer
On Error Resume Next
value = collList(strKey)
IsInCollection = Err.Number = 0
On Error GoTo 0
End Function
Sub epureFOREACHB1_JP()
Dim RgValeur As Range
Dim Rg As Range
Dim i As Long
Application.ScreenUpdating = False
i = rowStart ' was 1
For Each RgValeur In Sheets("feuil1").Range("a500:a10500") 'was Range("a1:a10000")
Set Rg = Range("B:B").Find(RgValeur.value)
If Rg Is Nothing Then
' i = Rg.Row
Set Rg = Range("C:C").Find(RgValeur.value)
If Rg Is Nothing Then
Range("C" & i).value = RgValeur.value
i = i + 1
End If
End If
Next
Application.ScreenUpdating = True
End Sub
Sub ClearResult()
Range(Cells(rowStart, colResult), Cells(rowEnd, colResult)).ClearContents
End Sub
Sub RandomValue()
Dim indRow As Integer
For indRow = rowStart To rowEnd
Cells(indRow, colSourceA) = CInt(Rnd() * rowEnd)
Cells(indRow, colSourceB) = CInt(Rnd() * rowEnd)
Next
End Sub |
Partager