Is there a better way than to use multiple ElseIf?

JeffGrant

Well-known Member
Joined
Apr 7, 2021
Messages
519
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I have a vba module that looks like this without the actual test conditions & code. This is the actual module with 17 elseif's, and several sub-if's. Each of these conditions are independant and are being tested for separately.
With Sheet1
If Then
ElseIf Then
ElseIf Then
ElseIf Then
ElseIf Then
ElseIf Then
ElseIf Then
ElseIf Then
ElseIf Then
ElseIf Then
ElseIf Or Or Or Then
If And Then
End If
If And Then
End If
If Then
End If
ElseIf Then
ElseIf Then
ElseIf Then
ElseIf Then
ElseIf Then
If Then
End If
ElseIf Then
End If
End With

I have looked at the arguments for and against multiple ElseIf's and Case, and it just seems to come down to personal preference. Given the spagetti code structures above, does anybody have an option on what would be a better way to test all these scenrios to give me better performance from the PC?

chat soon.

Jeff.
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
I would use case:
But to give you the code to use I would need specific details of what you're attempting to do.
 
Upvote 0
Have no idea if I dont know exactly what kind of If, else if are using.
Normally, there are several alternative solutions for nested if:
* use a worksheet table then vlookup
* use case select
* Or store all condition-result into an array
Try to give more information for us to see what we can help.
 
Upvote 0
With that many options I would also choose the Case option. However, whichever method you choose, there may be something that you can do to improve the speed.
How much improvement depends on your circumstances.

Take this code example

VBA Code:
Sub Test1()
  Dim a As Long, i As Long
  Dim t As Single
  t = Timer
  
  Randomize
  For i = 1 To 1000000
    a = 1 + Int(1000 * Rnd())
    If a = 1 Then
    ElseIf a = 2 Then
    ElseIf a = 3 Then
    ElseIf a = 4 Then
    ElseIf a = 5 Then
    Else
      'This code section will be used most of the time
    End If
  Next i
  Debug.Print "Code 1 took " & Format(Timer - t, "0.000 secs")
End Sub

Here we are choosing a random number from 1 to 1000 and we are doing that a million times.
Clearly, the vast majority of times the random number will be greater than 5 yet for every on of the million loops the code first checks if the number is 1 & if not checks for 2 etc.
In most cases these first 5 check are wasting time.

So, if you can order your If/ElseIf statements so that the most likely ones come first you don't waste so many checks.

VBA Code:
Sub Test2()
  Dim a As Long, i As Long
  Dim t As Single
  t = Timer
  
  Randomize
  For i = 1 To 1000000
    a = 1 + Int(1000 * Rnd())
    If a > 5 Then
      'This code section will be used most of the time
    ElseIf a = 5 Then
    ElseIf a = 4 Then
    ElseIf a = 3 Then
    ElseIf a = 2 Then
    Else
    End If
  Next i
  Debug.Print "Code 2 took " & Format(Timer - t, "0.000 secs")
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,028
Messages
6,128,393
Members
449,446
Latest member
CodeCybear

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