I have just replaced a speadsheet full of countif formulas with VBA code. I assumed this would make the sheet work faster. It does work faster when using it, as it no-longer recalculates constantly, but the initial calculation takes around 5 minutes, when it used to take 30-40 seconds.
Here is my code
Each ReportRow is a manager's name. There are ~600 of them.
Each DataRow is a staff member's details (name, last review, managers name etc). There are ~17000 of them
The code loops down each manager name on the report page, collects info about their staff from the data page, then enters these figures next to the manager name on the report page.
Is there anything I am missing that would make this code run faster? I would rather not go back to having thousands of countif forumulas
Thanks
Here is my code
Code:
Sub Count()
Dim Staff, ReviewsOut, PDPsOut, ReportRow, DataRow, LastRow, LastRow2 As Integer
Dim Manager As String
Dim ReviewDate As Date
Application.ScreenUpdating = False
Application.Cursor = xlWait
Application.DisplayStatusBar = True
Application.StatusBar = "Calculating..."
Sheets("Report").Select
ReviewDate = Range("B1").Value ' review date stored
Range("A3").Select
Selection.End(xlDown).Select
LastRow = ActiveCell.Row ' last row on report page
Sheets("Data").Select
Range("A1").Select
Selection.End(xlDown).Select
LastRow2 = ActiveCell.Row ' last row on data page
For ReportRow = 4 To LastRow ' loop through each manager
ReviewsOut = 0
PDPsOut = 0
Staff = 0 ' reset data values to 0
Sheets("Report").Select
Manager = Range("A" & ReportRow).Value ' manager name stored
Sheets("Data").Select
For DataRow = 2 To LastRow2 ' loop through each line on data page
If Range("Q" & DataRow).Value = Manager Then
Staff = Staff + 1 ' counts number of staff for this manager
End If
If Range("X" & DataRow).Value < ReviewDate And Range("Q" & DataRow).Value = Manager Then
ReviewsOut = ReviewsOut + 1 ' Count if last review older than review date
End If
If Range("Y" & DataRow).Value < ReviewDate And Range("Q" & DataRow).Value = Manager Then
PDPsOut = PDPsOut + 1 ' count if last PDP date older than review date
End If
Next DataRow
'' All data for this manager will now be stored in the variables
'' The following code enters the figures on the report page
Sheets("Report").Select
Range("B" & ReportRow).Value = Staff
Range("C" & ReportRow).Value = ReviewsOut
Range("F" & ReportRow).Value = PDPsOut
Next ReportRow
Application.Cursor = xlDefault
Application.StatusBar = False
Application.ScreenUpdating = True
End Sub
Each ReportRow is a manager's name. There are ~600 of them.
Each DataRow is a staff member's details (name, last review, managers name etc). There are ~17000 of them
The code loops down each manager name on the report page, collects info about their staff from the data page, then enters these figures next to the manager name on the report page.
Is there anything I am missing that would make this code run faster? I would rather not go back to having thousands of countif forumulas
Thanks
Last edited: