The code cycles thru the remainder of dArray
even after meeting the logic condition for setting an mArray
element to 'Anonymous'. This is inefficient since replacing the vbNullString
once is sufficient. Adding an Exit For
statement after replacing the vbNullString
value with 'Anonymous' will remove the inefficiency.
Regardless of the string comparisons, the condition for replacing the mArray
value depends directly on the condition that mArray(aRow,12) = vbNullString
. This is known prior to entering the dArray
loop. Check for this condition and skip iterating thru the nested loop altogether if the condition is not met.
Regardless of the string comparisons, the condition for replacing the mArray
value depends directly on mArray(aRow, 1) = dArray(bRow, 1)
. This is known the immediately after entering the dArray
loop. If the condition is not met, skip any other checks in the iteration.
Also, it looks like there is no reason to have an If
and ElseIf
based on the code fragment provided.
Below is the code with the changes described above. Declarations have been added to make the fragment compile.
Sub Test()
Dim aRow As Long
Dim bRow As Long
Dim dArray As Variant
Dim mArray As Variant
Dim toCheckFor As String
For aRow = 2 To UBound(mArray, 1)
If Not mArray(aRow, 12) = vbNullString Then
For bRow = LBound(dArray, 1) + 1 To UBound(dArray, 1) - 1
If mArray(aRow, 1) = dArray(bRow, 1) Then
toCheckFor = dArray(bRow, 15)
If toCheckFor = "issue SFC-anonymous" _
Or toCheckFor = "issue PRP-anonymous" _
Or toCheckFor = "add INV-PUR" _
Or toCheckFor = "add INV-SFC" Then
mArray(aRow, 12) = "Anonymous"
Exit For
End If
End If
Next bRow
End If
Next aRow
End Sub
The changes above should improve performance. However, the code contains both nested loops and If
statements. This makes the code very hard to read/understand and maintain. The code below eliminates the nested loop, improves readability, and will likely have an unnoticeable impact on performance.
Sub Test()
Dim aRow As Long
Dim dArray As Variant
Dim mArray As Variant
For aRow = 2 To UBound(mArray, 1)
If Not mArray(aRow, 12) = vbNullString Then
If MeetsConditionsToSetMArrayValue(dArray, mArray, aRow) Then
mArray(aRow, 12) = "Anonymous"
Exit For
End If
End If
Next aRow
End Sub
Private Function MeetsConditionsToSetMArrayValue(ByRef dArray As Variant, _
ByRef mArray As Variant, ByVal aRow As Long) As Boolean
Dim bRow As Long
For bRow = LBound(dArray, 1) + 1 To UBound(dArray, 1) - 1
If mArray(aRow, 1) = dArray(bRow, 1) Then
If ValueMeetsReplacementCriteria(dArray(bRow, 15)) Then
MeetsConditionsToSetMArrayValue = True
Exit Function
End If
End If
Next bRow
MeetsConditionsToSetMArrayValue = False
End Function
Private Function ValueMeetsReplacementCriteria(ByVal valueToCheck As String) As Boolean
ValueMeetsReplacementCriteria = _
valueToCheck = "issue SFC-anonymous" _
Or valueToCheck = "issue PRP-anonymous" _
Or valueToCheck = "add INV-PUR" _
Or valueToCheck = "add INV-SFC"
End Function