improve this sub and make it run faster. Sub CalculateFNRatio()
code
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Gantt") ' Change the sheet name as needed
Dim firstTaskRow As Long
firstTaskRow = 15
Dim lastRow As Long
lastRow = ws.Cells(ws.Rows.Count, "D").End(xlUp).Row
Dim i As Long
For i = firstTaskRow To lastRow
If ws.Cells(i, "F").value = 0 Then
ws.Cells(i, "T").Formula = "=count_FNtask/count_task"
End If
Next i
End Sub
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
To improve the CalculateFNRatio subroutine and make it run faster, you can use the following optimizations:
Avoid setting the formula in each iteration: Instead of setting the formula in each cell individually, you can calculate it once and apply the result directly to the relevant cells. This reduces the overhead of repeatedly applying formulas.
Use variables for repetitive calculations: Store the result of repetitive calculations in variables to avoid recalculating them multiple times.
Disable screen updating and calculations: Disabling screen updating and automatic calculations can speed up macro execution considerably, especially when working with large datasets.
Use Range objects: Instead of directly accessing cells in a loop, use Range objects to apply changes in bulk where possible.
Here’s the improved version of your subroutine:
vba
Sub CalculateFNRatio()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Gantt") ' Change the sheet name as needed
Dim firstTaskRow As Long
firstTaskRow = 15
Dim lastRow As Long
lastRow = ws.Cells(ws.Rows.Count, "D").End(xlUp).Row
Dim i As Long
Dim count_FNtask As Double
Dim count_task As Double
Dim fnRatio As Double
' Calculate the total number of tasks and FN tasks once
count_task = Application.WorksheetFunction.CountA(ws.Range(ws.Cells(firstTaskRow, "D"), ws.Cells(lastRow, "D")))
count_FNtask = Application.WorksheetFunction.CountIf(ws.Range(ws.Cells(firstTaskRow, "F"), ws.Cells(lastRow, "F")), 0)
' Prevent errors from division by zero
If count_task = 0 Then
fnRatio = 0
Else
fnRatio = count_FNtask / count_task
End If
' Disable screen updating and calculations to speed up the code
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
For i = firstTaskRow To lastRow
If ws.Cells(i, "F").Value = 0 Then
ws.Cells(i, "T").Value = fnRatio
End If
Next i
' Re-enable screen updating and calculations
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
Explanation:
Counting Tasks and FN Tasks: The number of tasks and FN tasks is calculated once before the loop, instead of performing this operation repeatedly inside the loop.
fnRatio Calculation: The ratio is calculated once and directly applied to the cells that meet the condition.
Error Handling: The code checks if count_task is zero to prevent division by zero errors.
Disabling Screen Updating and Calculation: This reduces the time spent on recalculating and refreshing the screen, which is especially helpful for large data sets.
This optimized subroutine should run faster, particularly when dealing with large amounts of data.