I am currently working with 3 files: New file (Sht)- in which the macro is written and the other two are wsSource and wsDest. There are about 1000 rows in the new file and about 1000 rows in wsSource. LastRowAT is the last row of the New File. LastRowKS is the last row of wsDest. The code matches particular conditions and then based on these conditions, copies the data from wsSource to wsDest. Now, because I am using a for loop and nested if, sometimes excel crashes and has to restart. Can I optimize this piece of code in any manner so that it takes less time to run and more importantly does not crash? P.S This is just sample data. The real data would involve about 100,000 rows in the wsSource file
For i = 2 To LastRowAT
If sht.Range("B" & i).Value = "Khusbhu Singh" And sht.Range("D" & i).Value = "Allocated" And sht.Range("C" & i).Value = Max_date Then
If sht.Range("A" & i).Value = Workbooks("CISF.xlsx").Worksheets("QUIDAM-INSURERS").Range("A" & i).Value Then
Set rngCopy = wsSource.Range("A" & i & ":V" & i)
rngCopy.Copy wsDest.Cells(LastRowKS, "I")
wsDest.Cells(LastRowKS, "I").Resize( _
rngCopy.Rows.Count, rngCopy.Columns.Count).Value = rngCopy.Value
wsDest.Range("A" & LastRowKS).Value = "Khusbhu Singh"
wsDest.Range("B" & LastRowKS).Value = Max_date
LastRowKS = LastRowKS + 1
End If
End If
Next i