The code below works, however it is taking 8.5 seconds which in the big scheme of my whole workbook macros is ernomous.
I have two input tables and one output table - Eligibility_Tbl </SPAN> and Exclusions_Tbl which determine whether </SPAN>Eligibility columns</SPAN> on the Data_Tbl </SPAN>is set to "Eligible" or "Not Eligible" for about 60000 rows.
Firstly the macro loops the Eligibility_Tbl </SPAN>to find grades that are not eligible for a salary increase and then loops the Exclusions_Tbl to find Staff_Id for employees that should always be listed on the Data_Tbl </SPAN> Eligibility as "Not Eligible" whether or not their grade qualifies them.
E.g.Eligibility_Tbl </SPAN>- Grade 1, grade 6 and grade 8 are "Eligible" but then individuals with Staff_Id A1234 and A3234 are "Not Eligible" on the Exclusions_Tbl. The Data_Tbl </SPAN> Eligibility column will have A1234 and A3234 as "Not Eligible"
Is there a more efficient way to loop through all three tables and achieve the above reducing from the current 8.5 seconds?
Public Function Exclusions()</SPAN></SPAN>
Dim StartTime As Double, SecondsElapsed As Double</SPAN></SPAN>
StartTime = Timer</SPAN></SPAN>
Dim Staff_Id As Variant, Exclusions_Staff_Id As Variant, Eligibility_Grade As Variant</SPAN></SPAN>
Dim Data_Tbl As Variant, Grade_Cluster As Variant</SPAN></SPAN>
Dim Ln As Long, x As Long, y As Long</SPAN></SPAN>
Dim Eligibility_Tbl As Variant, Exclusions_Tbl As Variant, Eligibility As Variant</SPAN></SPAN>
Staff_Id = Range("Data_Tbl[Staff Id]")</SPAN></SPAN>
Exclusions_Staff_Id = Range("Exclusions[Staff Id]")</SPAN></SPAN>
Eligibility_Grade = Range("Grade_Elig_Tbl[Corporate Grade]")</SPAN></SPAN>
Data_Tbl = Range("Data_Tbl")</SPAN></SPAN>
Grade_Cluster = Range("Data_Tbl[Grade Cluster]")</SPAN></SPAN>
Eligibility_Tbl = Range("Grade_Elig_Tbl")</SPAN></SPAN>
Exclusions_Tbl = Range("Exclusions")</SPAN></SPAN>
ReDim Eligibility(1 To UBound(Staff_Id), 1 To 1)</SPAN></SPAN>
For Ln = 1 To UBound(Staff_Id)</SPAN></SPAN>
For y = 1 To UBound(Eligibility_Grade)</SPAN></SPAN>
For x = 1 To UBound(Exclusions_Staff_Id)</SPAN></SPAN>
If Grade_Cluster(Ln, 1) = Eligibility_Tbl(y, 1) And Eligibility_Tbl(y, 2) = "Not Eligible" Then</SPAN></SPAN>
Eligibility(Ln, 1) = "Not Eligible - Grade"</SPAN></SPAN>
ElseIf Data_Tbl(Ln, 1) = Exclusions_Tbl(x, 1) Then</SPAN></SPAN>
Eligibility(Ln, 1) = "Not Eligible - Exclusions"</SPAN></SPAN>
ElseIf Grade_Cluster(Ln, 1) = Eligibility_Tbl(y, 1) And Eligibility_Tbl(y, 2) = "Eligible" Then</SPAN></SPAN>
Eligibility(Ln, 1) = "Eligible"</SPAN></SPAN>
End If</SPAN></SPAN>
Next x</SPAN></SPAN>
Next y</SPAN></SPAN>
Next Ln</SPAN></SPAN>
Range("Data_Tbl[Salary Increment Eligibility]") = Eligibility</SPAN></SPAN>
SecondsElapsed = Round(Timer - StartTime, 2)</SPAN></SPAN>
MsgBox "This code ran successfully in " & SecondsElapsed & " seconds", vbInformation</SPAN></SPAN>
End Function </SPAN></SPAN>
I have two input tables and one output table - Eligibility_Tbl </SPAN> and Exclusions_Tbl which determine whether </SPAN>Eligibility columns</SPAN> on the Data_Tbl </SPAN>is set to "Eligible" or "Not Eligible" for about 60000 rows.
Firstly the macro loops the Eligibility_Tbl </SPAN>to find grades that are not eligible for a salary increase and then loops the Exclusions_Tbl to find Staff_Id for employees that should always be listed on the Data_Tbl </SPAN> Eligibility as "Not Eligible" whether or not their grade qualifies them.
E.g.Eligibility_Tbl </SPAN>- Grade 1, grade 6 and grade 8 are "Eligible" but then individuals with Staff_Id A1234 and A3234 are "Not Eligible" on the Exclusions_Tbl. The Data_Tbl </SPAN> Eligibility column will have A1234 and A3234 as "Not Eligible"
Is there a more efficient way to loop through all three tables and achieve the above reducing from the current 8.5 seconds?
Public Function Exclusions()</SPAN></SPAN>
Dim StartTime As Double, SecondsElapsed As Double</SPAN></SPAN>
StartTime = Timer</SPAN></SPAN>
Dim Staff_Id As Variant, Exclusions_Staff_Id As Variant, Eligibility_Grade As Variant</SPAN></SPAN>
Dim Data_Tbl As Variant, Grade_Cluster As Variant</SPAN></SPAN>
Dim Ln As Long, x As Long, y As Long</SPAN></SPAN>
Dim Eligibility_Tbl As Variant, Exclusions_Tbl As Variant, Eligibility As Variant</SPAN></SPAN>
Staff_Id = Range("Data_Tbl[Staff Id]")</SPAN></SPAN>
Exclusions_Staff_Id = Range("Exclusions[Staff Id]")</SPAN></SPAN>
Eligibility_Grade = Range("Grade_Elig_Tbl[Corporate Grade]")</SPAN></SPAN>
Data_Tbl = Range("Data_Tbl")</SPAN></SPAN>
Grade_Cluster = Range("Data_Tbl[Grade Cluster]")</SPAN></SPAN>
Eligibility_Tbl = Range("Grade_Elig_Tbl")</SPAN></SPAN>
Exclusions_Tbl = Range("Exclusions")</SPAN></SPAN>
ReDim Eligibility(1 To UBound(Staff_Id), 1 To 1)</SPAN></SPAN>
For Ln = 1 To UBound(Staff_Id)</SPAN></SPAN>
For y = 1 To UBound(Eligibility_Grade)</SPAN></SPAN>
For x = 1 To UBound(Exclusions_Staff_Id)</SPAN></SPAN>
If Grade_Cluster(Ln, 1) = Eligibility_Tbl(y, 1) And Eligibility_Tbl(y, 2) = "Not Eligible" Then</SPAN></SPAN>
Eligibility(Ln, 1) = "Not Eligible - Grade"</SPAN></SPAN>
ElseIf Data_Tbl(Ln, 1) = Exclusions_Tbl(x, 1) Then</SPAN></SPAN>
Eligibility(Ln, 1) = "Not Eligible - Exclusions"</SPAN></SPAN>
ElseIf Grade_Cluster(Ln, 1) = Eligibility_Tbl(y, 1) And Eligibility_Tbl(y, 2) = "Eligible" Then</SPAN></SPAN>
Eligibility(Ln, 1) = "Eligible"</SPAN></SPAN>
End If</SPAN></SPAN>
Next x</SPAN></SPAN>
Next y</SPAN></SPAN>
Next Ln</SPAN></SPAN>
Range("Data_Tbl[Salary Increment Eligibility]") = Eligibility</SPAN></SPAN>
SecondsElapsed = Round(Timer - StartTime, 2)</SPAN></SPAN>
MsgBox "This code ran successfully in " & SecondsElapsed & " seconds", vbInformation</SPAN></SPAN>
End Function </SPAN></SPAN>