Goto Statement is incorrectly terminating the Nested IF Block - What am I doing wrong.

JeffGrant

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

I have this scenario.

If Then
elseif then
if condition A Then
Goto Label A
end if

Label A
elseif then
if condition B Then
Goto Label B
end if

Label B

etc for Label C, D & E


End if (code is jumping to here when condition A is true)

More code


The idea being that if condition A is true, then jump to the next Else If to check Condition B and so on.

But what I am finding is the if condition A is true, instead of following the Goto Label A instruction, all remaining Else If conditions are missed because the entire If block is terminated

I don't understand why the code is not jumping to the Label to continue the checking.

Any guidance is very much appreciated please.

Thanks in advance
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
You should never ever do that in code. If you think you need to, then your code needs to be completely restructured. It sounds like your conditions should be nested Ifs but I suspect there is more to it.
 
Upvote 0
I agree with Rory.

Best practice is GoTo statements should really be used in error handling. In well-written code, there is seldom ever to need something like that. Better to imbed the code within that section or use Sub Procedures.

Otherwise, if you use GotTos, you can end up with "spaghetti code", that can be difficult to follow and debug.

Also, sometimes you can use Case statements instead of Nested IFs, under the right circumstances. If checking one particular thing which can be equal to a number of different values, this is often much cleaner and easier to follow. See: MS Excel: How to use the CASE Statement (VBA). Not really sure if it makes sense in this situation because we do not know the exact details of what you are trying to do, but it is a good function to have in your toolbox, regardless!
 
Upvote 0
Hi Rory, you are correct, there is lot more to it than what I have explained. The model is complex and relies upon many Else If conditions with more nested Ifs conditions to check. I have considered switching to using Select Case option, but I dont see any benefit in it. So I think that is the wrong approach.

Apart from writing a separate sub for each IF condition, I really don't know how else to structure it.
 
Upvote 0
Hi Joe, yes I agree about spagetti code, but I do run a tight ship and it is easy for me to follow. What I dont understand is why the nested if code is terminating the if block rather than following the goto statement.
 
Upvote 0
It's very hard to answer such a generic question unfortunately (other than to say that Goto is unlikely ever to be the answer, and definitely not in what you posted). I will say that if multiple conditions all need to be checked, then Else statements don't belong there.
 
Upvote 0
It is hard to debug your actual code without actually seeing it.
I think your generic representation is too watered down to make any real sense of it.
 
Upvote 0
Would the suggestion be to rewrite the code and put each else if into a separate sub?
 
Upvote 0
Would the suggestion be to rewrite the code
Probably, but as Joe and I have both said, we can't really give the best advice based on vague pseudo code. ;)
 
Upvote 0
The reason I dont put the code up here is because it is very long, not complex for gents like yourselves, but following it would be a headach and a half :)
 
Upvote 0

Forum statistics

Threads
1,214,814
Messages
6,121,711
Members
449,049
Latest member
THMarana

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