I wrote a VBA macro & I want to improve the performance because the macro takes ages to run.
I think that the running performance is impacted by the
For Each rCell In .Range("O3:O" & Range("O" & Rows.Count).End(xlUp).Row) which intend to limit the loop up to the first empty row.
Sub E_Product_Density_Check()
Dim ws As Worksheet
Set Vws = ThisWorkbook.Sheets("Variables")
Sheets("Sheet1").Select
Application.ScreenUpdating = False
For Each ws In ActiveWorkbook.Worksheets
If ws.Name <> "Variables" Then
Application.DecimalSeparator = ","
ws.Activate
With ActiveSheet
For Each rCell In .Range("O3:O" & Range("O" & Rows.Count).End(xlUp).Row)
For Each iCell In .Range("N3:N" & Range("N" & Rows.Count).End(xlUp).Row)
For Each xCell In .Range("M3:M" & Range("M" & Rows.Count).End(xlUp).Row)
For Each yCell In .Range("L3:L" & Range("L" & Rows.Count).End(xlUp).Row)
If (rCell.Value / ((iCell.Value * xCell.Value * yCell.Value) / 1000000)) <= Application.WorksheetFunction.VLookup(ActiveSheet.Name, Vws.Range("A1:E10"), 5, False) Then
rCell.Interior.Color = vbYellow
Else
rCell.Interior.Color = vbWhite
End If
Next yCell
Next xCell
Next iCell
Next rCell
End With
End If
Next ws
End Sub
n^4gets very large very fast. In any event, if the purpose is to color cells -- why not just use conditional formatting?