Confusion about how call works within an if/elseif statement

Moonbeam111

Board Regular
Joined
Sep 24, 2018
Messages
64
Office Version
  1. 365
  2. 2010
Good afternoon all

I have this simple watered down code here: (Just for demonstration purposes)

VBA Code:
If Range("G41") = 'rest of code here' Then
Call macro1
Call macro2
ElseIf Range("G41") = 'rest of code here' then
msgbox 'message here'
ElseIf Range("G41") = 'rest of code here' then
msgbox 'message here'
ElseIf Range("G41") = 'rest of code here' then
Call macro1_alternate
Call macro2_alternate
End If

Im confused about it. Basically when I run it, it doesnt test each elseif statement in sequence. Apparently after macro2 is called the code immediately jumps to end if. Is this normal VBA behavior or is something in my code wrong? If not, what workaround is there so that I get each elseif statement tested? I prefer not to use select case if possible.
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
If macro2 was executed, that means the first IF test was passed. When a test is passed, the IF ends. It'll only go to the next ELSEIF if the prior tests failed.
 
Upvote 0
That explains it, thank you. Ill have to restructure my code. Maybe put an end if after macro2.
 
Upvote 0

Forum statistics

Threads
1,216,033
Messages
6,128,427
Members
449,450
Latest member
gunars

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