I'm looking for some enlightening, since I'm still learning to code in VBA. I'm running code to create a daily report. It has to compare dates and check certain values in those dates and based on that I get my info. The info is gathered from three worksheets that are on the same workbook. It's working since it does what I wanted it to do, and at the start of the month it was working "fast". But now since my data is getting bigger, it also became slow and I think it's because I didn't optimize it and I'm running it on a desktop with an Intel Core i7-7700.
I will post a fragment of the code where I notice it's getting slow and the start of the code for variables.
Sub gen_informe()
Dim wsrgcmes As Worksheet
Dim wshtte As Worksheet
Dim wsstats As Worksheet
Dim rdate As Range
Dim celdate As Range
Dim idtask As Range
Dim rtask As Range
Dim idcaso As Range
Dim rcaso As Range
Dim rstats As Range
Dim idstats As Range
Dim x As Long
Dim i As Double
Dim fregistro As Double
Dim coninc As Integer
Dim conser As Integer
Dim fcierre As Double
Dim ansin As String
Dim ansout As String
Set wsrgcmes = ThisWorkbook.Worksheets("ResumenGeneralCasosMES")
Set wshtte = ThisWorkbook.Worksheets("HistoricoTareas")
Set wsstats = ThisWorkbook.Worksheets("SolucionadosTATS")
With wsrgcmes
Set rdate = .Range("W2", .Cells(.Rows.count, .Columns("W:W").Column).End(xlUp))
Set rcaso = .Range("B2", .Cells(.Rows.count, .Columns("B:B").Column).End(xlUp))
End With
With wshtte
Set rtask = .Range("B2", .Cells(.Rows.count, .Columns("B:B").Column).End(xlUp))
End With
With wsstats
Set rstats = .Range("E2", .Cells(.Rows.count, .Columns("E:E").Column).End(xlUp))
End With
Sheets("Informe").Range("B4").Select
For i = CDbl(DateSerial(Year(Date), Month(Date), 1)) To CDbl(Date)
For Each celdate In rdate
fregistro = CDbl(Int(celdate.Value))
If fregistro = i Then
Select Case celdate.Offset(0, -19).Value
Case "INCIDENTE"
coninc = coninc + 1
Case "LLAMADA DE SERVICIO"
conser = conser + 1
End Select
End If
Next celdate
ActiveCell.Offset(0, x).Value = coninc
ActiveCell.Offset(1, x) = conser
x = x + 1
coninc = 0
conser = 0
Next i
coninc = 0
conser = 0
i = 0
x = 0
Sheets("Informe").Range("B12").Select
For i = CDbl(DateSerial(Year(Date), Month(Date), 1)) To CDbl(Date)
For Each celdate In rdate
fregistro = CDbl(Int(celdate.Value))
fcierre = CDbl(Int(celdate.Offset(0, 2).Value))
If fregistro > CDbl(DateSerial(Year(Date), Month(Date), 0)) And fcierre = i Then
Select Case celdate.Offset(0, -19).Value
Case "INCIDENTE"
coninc = coninc + 1
Case "LLAMADA DE SERVICIO"
conser = conser + 1
End Select
End If
Next celdate
ActiveCell.Offset(0, x).Value = coninc
ActiveCell.Offset(1, x) = conser
x = x + 1
coninc = 0
conser = 0
Next i
coninc = 0
conser = 0
i = 0
x = 0
Sheets("Informe").Range("B19").Select
For i = CDbl(DateSerial(Year(Date), Month(Date), 1)) To CDbl(Date)
For Each celdate In rdate
fregistro = CDbl(Int(celdate.Value))
fcierre = CDbl(Int(celdate.Offset(0, 2).Value))
If fregistro < CDbl(DateSerial(Year(Date), Month(Date), 1)) And fcierre = i Then
Select Case celdate.Offset(0, -19).Value
Case "INCIDENTE"
coninc = coninc + 1
Case "LLAMADA DE SERVICIO"
conser = conser + 1
End Select
End If
Next celdate
ActiveCell.Offset(0, x).Value = coninc
ActiveCell.Offset(1, x) = conser
x = x + 1
coninc = 0
conser = 0
Next i
contask = 0
i = 0
x = 0
Sheets("Informe").Range("B27").Select
For i = CDbl(DateSerial(Year(Date), Month(Date), 1)) To CDbl(Date)
On Error Resume Next
For Each idtask In rtask
With Application
Set idcaso = .Index(rcaso, .Match(idtask.Offset(0, -1).Value, rcaso, 0))
End With
fregistro = CDbl(Int(idcaso.Offset(0, 21).Value))
If fregistro = i Then
contask = contask + 1
End If
Next idtask
ActiveCell.Offset(0, x).Value = contask
x = x + 1
contask = 0
Next i
contask = 0
i = 0
x = 0
For i = CDbl(DateSerial(Year(Date), Month(Date), 1)) To CDbl(Date)
On Error Resume Next
For Each idtask In rtask
With Application
Set idcaso = .Index(rcaso, .Match(idtask.Offset(0, -1).Value, rcaso, 0))
End With
fregistro = CDbl(Int(idcaso.Offset(0, 21).Value))
fcierre = CDbl(Int(idtask.Offset(0, 7).Value))
If fregistro > CDbl(DateSerial(Year(Date), Month(Date), 0)) And fcierre = i Then
contask = contask + 1
End If
Next idtask
ActiveCell.Offset(1, x).Value = contask
x = x + 1
contask = 0
Next i
contask = 0
i = 0
x = 0
For i = CDbl(DateSerial(Year(Date), Month(Date), 1)) To CDbl(Date)
On Error Resume Next
For Each idtask In rtask
With Application
Set idcaso = .Index(rcaso, .Match(idtask.Offset(0, -1).Value, rcaso, 0))
End With
fregistro = CDbl(Int(idcaso.Offset(0, 21).Value))
fcierre = CDbl(Int(idtask.Offset(0, 7).Value))
If fregistro < CDbl(DateSerial(Year(Date), Month(Date), 1)) And fcierre = i Then
contask = contask + 1
End If
Next idtask
ActiveCell.Offset(2, x).Value = contask
x = x + 1
contask = 0
Next i
From here on, it starts getting slower, these last two for
-loops are iterating a lot it seems. I used similar for-loops along the code, maybe poor optimization on my part.
i = 0
x = 0
ansin = 0
ansout = 0
Sheets("Informe").Range("B42").Select
For i = CDbl(DateSerial(Year(Date), Month(Date), 1)) To CDbl(Date)
On Error Resume Next
For Each idstats In rstats
With Application
Set idcaso = .Index(rcaso, .Match(idstats.Value, rcaso, 0))
End With
fcierre = CDbl(Int(idcaso.Offset(0, 23).Value))
If fcierre = i And idstats.Offset(0, -1).Value = "Incidente" Then
Select Case idstats.Offset(0, 20).Value
Case "S"
ansin = ansin + 1
Case "N"
ansout = ansout + 1
End Select
End If
Next idstats
ActiveCell.Offset(0, x).Value = ansin
ActiveCell.Offset(1, x) = ansout
x = x + 1
ansin = 0
ansout = 0
Next i
i = 0
x = 0
ansin = 0
ansout = 0
Sheets("Informe").Range("B49").Select
For i = CDbl(DateSerial(Year(Date), Month(Date), 1)) To CDbl(Date)
On Error Resume Next
For Each idstats In rstats
With Application
Set idcaso = .Index(rcaso, .Match(idstats.Value, rcaso, 0))
End With
fcierre = CDbl(Int(idcaso.Offset(0, 23).Value))
If fcierre = i And idstats.Offset(0, -1).Value = "Requerimiento" Then
Select Case idstats.Offset(0, 20).Value
Case "S"
ansin = ansin + 1
Case "N"
ansout = ansout + 1
End Select
End If
Next idstats
ActiveCell.Offset(0, x).Value = ansin
ActiveCell.Offset(1, x) = ansout
x = x + 1
ansin = 0
ansout = 0
Next i
End Sub