Nested If is terminating total if block

JeffGrant

Well-known Member
Joined
Apr 7, 2021
Messages
516
Office Version
  1. 365
Platform
  1. Windows
Hi All, I am confused by this one.

I have a Nested If statement like this:

The variable SetWeightIgnor is Dim as string at the top of the module.

ElseIf.....

If SetWeightIgnore = "Yes" Then
GoTo KeepMovingOn
Else
Code...
End If


KeepMovingOn:
Elseif....

Now for whatever reason, when the Nested If is true, the Goto is ignored and the entire IF block is terminated, when all I want it to do is to jump to the appropriate Label.

I have tried using various methods to manage the flow of the control using a Select Case and a Function and I get the same result.

Am I missing something obvious?

Thanks in advance
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
I'm pretty sure we have had this conversation before. You really need to stop using Goto to jump in and out of code blocks.
 
Upvote 0
Hi Rory, (I has a suspicion that you were going to say that :))

Under your advice, I have changed 99% of the previous structure and removed almost all of the GoTos. However, in this case if the variable is true, then I need the code to jump to the next Elseif.

I understand that using Goto makes code difficult to follow, following code is not the issue here. The issue is that the Nested If simply terminates the entire IF Block and I don't understand why.

Maybe a better way to approach this is to think in reverse ie. consider the code to be:

If SetWeightIgnore <> "Yes then or maybe use the If Not concept? That would get rid of this GoTo...
 
Upvote 0
Ok, that got it. I used

If Not SetWeightIgnore <> "Yes" Then
code here.....
End If

I am not all that comfortable with reverse thinking like this, but if it works - I am happy.
 
Upvote 0
It's not just difficult to follow. The rules for how VBA works if you jump around blocks using Goto are extremely hard to interpret. For example:

If a <goto-statement> defined outside the <if-statement> causes a <statement> within
<statement-block> to be executed, the <boolean-expression> is not evaluated. A <goto-
statement> may also cause execution to leave the <statement-block>. If a later <goto-
statement> causes execution to re-enter the <statement-block>, the behavior is as specified by
the rules defined for execution of an <if-statement>.

You appear to be trying to overrule the natural order of execution, which is pretty much never a good idea. You probably need to be adding an And condition to the ElseIf prior to your nested If
 
Upvote 0
Solution
Thanks for that answer, that was going to be my next question, so you have answered it an advance :)

and you're bang on... there is an AND statement in the preceding Elseif

Just spent the last week picking up M language as well. The data supplier is dropping the csv data file availability and only supplying Json. All good fun in the learning world. :)

Thanks
 
Upvote 0
Thanks for that answer, that was going to be my next question, so you have answered it an advance :)

and you're bang on... there is an AND statement in the preceding Elseif
The marked solution has been switched with the post that solves the issue as a general approach to help future readers in a similar situation.
 
Upvote 0

Forum statistics

Threads
1,215,076
Messages
6,122,988
Members
449,093
Latest member
Mr Hughes

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