Optimizing function for comparing array elements

mathsbeauty

Board Regular
Joined
Apr 23, 2011
Messages
89
Here is the function. It works. It runs hundreds of times during some VBA procedure. Can the function be optimized? It accepts temparray as an array and identifies the first occurrence of "O", "A+" etc. in elements of array. I think it can be optimized as it compares tempArray(i) in every run of loop with "O", "A+" etc.

VBA Code:
function grade(tempArray)
For i = 0 To 10
If tempArray(i) = "O" Or tempArray(i) = "A+" Or tempArray(i) = "A" Or tempArray(i) = "B+" Or tempArray(i) = "B" Or tempArray(i) = "C" Or tempArray(i) = "D" Or tempArray(i) = "F" Then
grade = tempArray(i)
Exit Function
End If
Next i
grade = ""
end Function

Any help will be appreciated. Thanks.
 
Last edited:

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Perhaps:
VBA Code:
Function grade(tempArray)
Dim tx As String
tx = "|O|A+|A|B+|B|C|D|F|"

   For i = 0 To 10
       If InStr(tx, "|" & tempArray(i) & "|") Then
           grade = tempArray(i)
           Exit Function
       End If
   Next i

   grade = ""
End Function
 
Upvote 0
Solution
You're welcome, glad to help & thanks for the feedback.:)
 
Upvote 0

Forum statistics

Threads
1,215,541
Messages
6,125,413
Members
449,223
Latest member
Narrian

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top