I've been looking the web and this forum but i can't seem to find a solution to my problem.
I have a table with this data:
EDITED THE CODE
I have this code:
Sub HorariosReal()
Dim LastRow As Long, Horario As String, i As Long, arr1 As Variant, a As Long, arrFichajes() As String, _
arrFinal() As String, Valor1 As Single, Valor2 As Single, x As Long, y As Long, Done As Boolean
Set YaHecho = New Scripting.Dictionary
'Primero metemos en un array la gente con horario
LastRow = ws2.Range("A1").End(xlDown).Row
arr1 = ws2.Range("A2:A" & LastRow).Value2
'Convertimos a valores los datos de fichajes y los reemplazamos
LastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
With ws.Range("F2:J" & LastRow)
.FormulaR1C1 = "=IFERROR(VALUE(RC[-5]),RC[-5])"
.Value = .Value
.Cut Destination:=ws.Range("A2")
End With
'Miramos si tiene programación
With ws.Range("F2:F" & LastRow)
.FormulaR1C1 = "=IFERROR(VLOOKUP(RC[-4],Horarios!C1:C37,MATCH(Fichajes!RC[-5],Horarios!R1C1:R1C37,0),FALSE),""No aparece en programación"")"
.Value = .Value
End With
'metemos los datos en un array
ReDim arrFichajes(2 To LastRow, 1 To 6)
ReDim arrFinal(2 To LastRow, 1 To 5)
For i = 2 To UBound(arrFichajes, 1)
For a = 1 To UBound(arrFichajes, 2)
arrFichajes(i, a) = ws.Cells(i, a)
If a = 3 Or a = 4 Then arrFichajes(i, a) = Format(ws.Cells(i, a), "hh:mm")
If a = 5 Then
Valor1 = Application.Round(ws.Cells(i, a), 2)
arrFichajes(i, a) = Valor1
End If
Next a
Next i
x = 2
y = 2
For i = 2 To UBound(arrFichajes, 1)
Horario = arrFichajes(i, 3) & "-" & arrFichajes(i, 4)
Valor1 = arrFichajes(i, 5)
Done = CompruebaDiccionario(arrFichajes(i, 1) & arrFichajes(i, 2))
If Done Then
arrFinal(Llave, 3) = arrFinal(Llave, 3) & "/" & Horario
Valor1 = arrFinal(Llave, 5)
Valor2 = arrFichajes(i, 5)
Valor1 = Valor1 + Valor2
arrFinal(Llave, 5) = Valor1
Else
arrFinal(x, 1) = arrFichajes(i, 1)
arrFinal(x, 2) = arrFichajes(i, 2)
arrFinal(x, 3) = Horario
arrFinal(x, 4) = arrFichajes(i, 6)
arrFinal(x, 5) = Valor1
YaHecho.Add y, arrFinal(x, 1) & arrFinal(x, 2)
y = y + 1
x = x + 1
End If
Next i
ws.Range("A2:E" & LastRow).ClearContents
ws.Range("A2:E" & UBound(arrFinal, 2)).Value = arrFinal
LastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
With ws.Range("F2:F" & LastRow)
.FormulaR1C1 = "=IFERROR(VALUE(RC[-1]),RC[-1])"
.Value = .Value
.Cut Destination:=ws.Range("E2")
End With
End Sub
Added this function to loop through a dictionary:
Function CompruebaDiccionario(Ejemplo As String) As Boolean
Dim Key As Variant
For Each Key In YaHecho.Keys
If YaHecho(Key) = Ejemplo Then
CompruebaDiccionario = True
Llave = Key
Exit For
End If
Next Key
End Function
The IDs are just a sample, but the thing is that one ID (Column B) can have multiple entries (Columns C and D) on the same day (Column A).
This is data from workers, their in (Column C) and outs (Column D) from their work, I need to merge all the entries from one worker on the same day in one row (on column C), then on column D find his schedule.
The code works ok, but extremely slow. I noticed that if I keep stopping the code, it goes faster (¿?¿? is this possible).
I decided to work with arrays because this is one week and it has 35k + rows, still it takes ages to end.
What I am asking is if there is something wrong on my code that slows down the process. Any help would be appreciated.
Thanks!
Edit:
I'm using this sub before this one is called:
Sub AhorroMemoria(isOn As Boolean)
Application.Calculation = IIf(isOn, xlCalculationManual, xlCalculationAutomatic)
Application.EnableEvents = Not (isOn)
Application.ScreenUpdating = Not (isOn)
ActiveSheet.DisplayPageBreaks = False
End Sub