mercredi 7 juin 2017

IF OR statement faster than two if statements?

I was wondering if there was a general rule regarding the sequence of several IF statements, taking ORstatements and & combination into consideration.

e.g. below is my initial thought about the fastest way

Dim DataRCntr As Long
Dim MyRange As Range
Dim Target As String
Dim DataArr() As Variant
Dim StrArr() As Variant

DataArr = MyRange.Value

StrArr = Array(Target, "High", "Major", Target & "High", Target & "Major")

For DataRCntr = LBound(DataArr, 1) To UBound(DataArr, 1)

    If DataArr(DataRCntr, 4) & DataArr(DataRCntr, 11) = StrArr(2) Or _
    DataArr(DataRCntr, 4) & DataArr(DataRCntr, 11) = StrArr(3) Then

        If DataArr_Current(DataRCntr, 20) < 0.9 Then

        'Do Something

        End If

    End If

Next DataRCntr

Example of a few things i was thinking about:

  1. In first IF statement, you could split it up in two so you first check for DataArr(DataRCntr, 4)=StrArr(1) to avoid the OR statement if the first doesn't match
  2. You could create a String combining DataArr(DataRCntr, 4) & DataArr(DataRCntr, 11)before the IF statement

Aucun commentaire:

Enregistrer un commentaire