I am trying to loop over a sheet of nearly 700 lines according to entries of a mapping (index i),each iteration of loop i takes a very long time (long time looping over j) .What can be the cause?I write Macros nearly every day , never experienced this issue. Also I tried to leave only the first line in the loop j , but kept experiencing this issue
The business need : Is to extract all columns in the sheet where Column C contains value that is in the mapping sheet(i.e. satisfying this condition PacketWork = Sheets(sheetname).Cells(j, Column3).Value where column3 is "C"
For i = 2 To lastRowLevelMapping
PacketWork = Sheets("Mapping").Cells(i, "A").Value
For j = 2 To LastRowSheet
If (PacketWork = Sheets(sheetname).Cells(j, Column3).Value) Then
Sheets("Source").Cells(writer_counter, "A").Value = Sheets(sheetname).Cells(j, Column1).Value
Sheets("Source").Cells(writer_counter, "B").Value = Sheets(sheetname).Cells(j, Column2).Value
Sheets("Source").Cells(writer_counter, "C").Value = Sheets(sheetname).Cells(j, Column3).Value
Sheets("Source").Cells(writer_counter, "D").Value = Sheets(sheetname).Cells(j, Column4).Value
data = Sheets(sheetname).Cells(j, Column5).Value
If (IsNumeric(data)) Then
Sheets("Source").Cells(writer_counter, "E").Value = Round(data, 2)
Else
Sheets("Source").Cells(writer_counter, "E").Value = data
End If
data = Sheets(sheetname).Cells(j, Column6).Value
If (IsNumeric(data)) Then
Sheets("Source").Cells(writer_counter, "F").Value = Round(data, 2)
Else
Sheets("Source").Cells(writer_counter, "F").Value = data
End If
data = Sheets(sheetname).Cells(j, Column7).Value
If (IsNumeric(data)) Then
Sheets("Source").Cells(writer_counter, "G").Value = Round(data, 2)
Else
Sheets("Source").Cells(writer_counter, "G").Value = data
End If
data = Sheets(sheetname).Cells(j, Column8).Value
If (IsNumeric(data)) Then
Sheets("Source").Cells(writer_counter, "H").Value = Round(data, 2)
Else
Sheets("Source").Cells(writer_counter, "H").Value = data
End If
data = Sheets(sheetname).Cells(j, Column9).Value
If (IsNumeric(data)) Then
Sheets("Source").Cells(writer_counter, "I").Value = Round(data, 2)
Else
Sheets("Source").Cells(writer_counter, "I").Value = data
End If
If (IsDate(Sheets(sheetname).Cells(j, Column10).Value)) Then
Sheets("Source").Cells(writer_counter, "P").Value = Format(Sheets(sheetname).Cells(j, Column10).Value, "dd/mm/yyyy")
Else
Sheets("Source").Cells(writer_counter, "P").Value = Sheets(sheetname).Cells(j, Column10).Value
End If
Sheets("Source").Cells(writer_counter, "P").NumberFormat = "dd/mm/yyyy"
If (Sheets("Source").Cells(writer_counter, "E").Value <> 0) Then
Sheets("Source").Cells(writer_counter, "L").Value = Round((Sheets("Source").Cells(writer_counter, "F").Value / Sheets("Source").Cells(writer_counter, "E").Value) * 100, 2)
Sheets("Source").Cells(writer_counter, "M").Value = Round((Sheets("Source").Cells(writer_counter, "G").Value / Sheets("Source").Cells(writer_counter, "E").Value) * 100, 2)
Else
Sheets("Source").Cells(writer_counter, "L").Value = 0
Sheets("Source").Cells(writer_counter, "M").Value = 0
End If
Sheets("Source").Cells(writer_counter, "L").NumberFormat = "0.00"
Sheets("Source").Cells(writer_counter, "M").NumberFormat = "0.00"
If (Sheets("Source").Cells(writer_counter, "L").Value > 100) Then
Sheets("Source").Cells(writer_counter, "L").Value = 100
End If
If (Sheets("Source").Cells(writer_counter, "M").Value > 100) Then
Sheets("Source").Cells(writer_counter, "M").Value = 100
End If
If (Contains(Mapping2Collection, Sheets(sheetname).Cells(j, Column4).Value)) Then
Sheets("Source").Cells(writer_counter, "N").Value = Mapping2Collection(Sheets(sheetname).Cells(j, Column4).Value)
End If
Sheets("Source").Cells(writer_counter, "K").Value = Sheets("Mapping").Cells(i, "D").Value
Sheets("Source").Cells(writer_counter, "J").Value = Sheets("Mapping").Cells(i, "C").Value
Sheets("Source").Cells(writer_counter, "O").Value = Sheets("Mapping").Cells(i, "B").Value
writer_counter = writer_counter + 1
End If
Next j
Next i