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
 
There are many else if blocks that look like this



VBA Code:
Label206:
            KnockOutError = "206"
            'check for count of trickster horses
        ElseIf .Range("EX7").Value > .Range("FC25").Value Then
            
            If Sheet14.Range("D13").Value >= Sheet1.Range("$E$85").Value Then
                MsgBox "D13"
                GoTo Label206A
            End If
            
            'MsgBox "The count of Trickster horses exceeds the number allowable for this number of entrants"
            frmMessageBox.Label2 = "The count of Trickster horses exceeds the number allowable for this number of entrants"
            frmMessageBox.Show
            Call LoadErrorForms
            Answer = MsgBox("Did a selected horse come first", vbYesNo + vbQuestion, "More Detail Please!")
            If Answer = vbYes Then
                KnockOutError = "The count of Danger horses exceeds the number allowable (EX7 & FC25)- Yes"
                Sheet14.Range("B13").Value = Sheet14.Range("B13").Value + 1
            Else: KnockOutError = "The count of Danger horses exceeds the number allowable (EX7 & FC25) - No"
                Sheet14.Range("C13").Value = Sheet14.Range("C13").Value + 1
                Call UnloadErrorForms
                GoTo JumpCopyFailedResults
            End If
            Call ResultsN
            Call RecordError
            Call Skip1
            GoTo SubEnd
                
Label206A:
        
            'check for horses where their last race was a group/listed race and they are NOT selected
        ElseIf .Range("CN2").Value = "No" Then
            MsgBox "Horses in this race that were last in a Listed or Group Race but have NOT been selected"
            KnockOutError = "Horses in this race that were last in a Listed or Group Race but have NOT been selected"
            Call RecordError
            Call Skip1
            GoTo SubEnd

            'check for danger horses
        ElseIf .Range("DL2").Value = "No" Then
            'MsgBox "There is a Danger Horse is this mob" & vbNewLine & "100% Line Wt = " & .Range("dl5").Value & vbNewLine & "0% Line Wt = " & .Range("dl8").Value
            KnockOutError = "Danger Horse: 100% = " & .Range("dl5").Value & ", 0% = " & .Range("dl8").Value
            Call LoadErrorForms
            Answer = MsgBox("Did a selected horse come first", vbYesNo + vbQuestion, "More Detail Please!")
            If Answer = vbYes Then
                KnockOutError = "There is a Danger Horse (DL2) - Yes"
                Sheet14.Range("B35").Value = Sheet14.Range("B35").Value + 1
            Else: KnockOutError = "There is a Danger Horse (DL2) - No"
                Sheet14.Range("C35").Value = Sheet14.Range("C35").Value + 1
                GoTo JumpCopyFailedResults
                Call UnloadErrorForms
            End If
            Call ResultsN
            Call RecordError
            Call Skip1
            GoTo SubEnd
 
Upvote 0

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Would the suggestion be to rewrite the code and put each else if into a separate sub?
Without knowing the details of exactly what you are trying to do, that would be my first inclination, and what I mentioned in my first reply.

Better to imbed the code within that section or use Sub Procedures.
 
Upvote 0
Joe, what do you mean by: Better to imbed the code within that section ?
 
Upvote 0
Joe, what do you mean by: Better to imbed the code within that section ?
Whatever code you have in your "GoTo" block, just move it under the IF.

If it is too long, or you may need to re-use it, then I would recommend instead creating a Sub Procedure and calling it from the code in your IF block.
 
Upvote 0
Oh. Ok. Now I get it.

I think it is time for thinking caps to be put on because Rory has indicated on a few occasions now that I need to rethink the structure of the project and now that you are saying the same thing and my code is playing up, maybe it is time that I listened to you good folk :) and do as I am told........

Thanks for your help guys. Very Much appreciated.
 
Upvote 0
Yep, I'm feeling slightly ill looking at all those Goto lines...
 
Upvote 0
Hi Guys

I have spent the time and restructured my model. I have pulled out all of the Elseif's and removed all but one Goto. So all good there :)

However, this has introduced two new issues.

1. Because the ElseIf condition is now placed in the Called Sub, instead of being in the Calling Sub, when the execution is returned to the Calling Sub, the next Called Sub is evaluated and so on. Previously, when the ElseIf was in Calling Sub, the GoTo statement would move to the end of the sub and everybody was happy.

It is easy enough to put the ElseIf condition back into the Calling Sub, but that does not alleviate the GoTo statements or the number of ElseIfs that caused the original concerns.

My question is this: How do I move the execution point back to a specific loaction in the Calling Sub from the Called Sub to evoid evaluation all of the other Called Subs.

2. The second is that because I now use several layers of Called Sub's, then to get out of the these subs, somewhere along the line I have to use an END statenment. No drama there, except that the End statement also kills an active User Form that is required. So I am finding that I have to continualy show the user form manually. Is there any way to make sure that the userform always stays open and active?

Thanks
 
Upvote 0
I really cant see anyway around having this type of satructure:

If condition is true then
 
Upvote 0
It sounds like you should be using functions rather than subs. Your function can return True/False and the calling routine simply checks that return value before proceeding.
 
Upvote 0
another approach could be to analyse what the different actions are with your logic tree and workout all the conditions that apply to each sub that you want to call and do the logic using boolean logic instead of nested if, this could simplify the whole thing. Just as example this is the sort of thing I am thinking taken from a simplified version of your posted code:
VBA Code:
flag1 = .Range("EX7").Value > .Range("FC25").Value And Sheet14.Range("D13").Value >= Sheet1.Range("$E$85").Value And .Range("CN2").Value = "No"
flag2 = .Range("EX7").Value > .Range("FC25").Value And Sheet14.Range("D13").Value >= Sheet1.Range("$E$85").Value And .Range("DL2").Value = "No"
If flag2 Then Call ResultsN
If flag1 Or flag2 Then
            Call RecordError
            Call Skip1
End If
 
Upvote 0

Forum statistics

Threads
1,215,054
Messages
6,122,895
Members
449,097
Latest member
dbomb1414

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