If Block terminating early

JeffGrant

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

This code is part of larger If, Then, Elseif Block.

At this particular ElseIf condition, a sub named SetWeightCalcs is called. The sub returns a Yes/No variable called SetWeightIgnore. The sub is returning the correct value of Yes or No. No problem.

However, regardles of whether I use a Nest If, a Select Case or other various combinations this code ALWAYS terminates the entire If Block. Even in this example where the Case "Yes" only jumps to the next Label100. (whis is over kill for testing purposes).

Please note in this example, the If and End If statments are commented out.

In this particular example, steping through the code successfully goes to Case "Yes", the successfully goes to "Goto Label100", then it jumps to the End If of the entire If block.

I have no idea why.

Can you good folk please help me out with this one.

VBA Code:
            SetWeightIgnore = "" ' clear the value before entering the sub
            Call SetWeightCalcs
            'MsgBox SetWeightIgnore
     '      If SetWeightIgnore <> "Yes" Then
            Select Case SetWeightIgnore
                Case "No"
                    Call ResultsN
                    Call RecordError
                    Call Skip1
                    GoTo SubEnd
                Case "Yes"
                    GoTo Label100
      '       End If
             End Select
            
Label100:
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
You really have to stop using Goto. ;)
 
Upvote 0
Hi Rory, I have spent the last week completly restructuring the model, all gotos, except where absolutely necessary have been removed. There is a Goto SubEnd in the above code, but because that is under the Select Case "No" option, it does not get acted upon.

The GoTo Label100 in the above code is there for no other reason than to explain that the code terminates the entire If block at this point rather than jump to the Label100. It didnt make any difference whether I included the Case "Yes" or not. Or whether I used a Nest If or a Select Case, the result is the same. Every combination jumped to the End If at the end of the primary block.

As much as I understand that Goto is a no-no, this is used as an example only here.

I have just tried bringing the SetWeightsCalcs sub into the same module and declaring the SetWeightIgnore variable with a Dim instead of a Public and the same occures.

It successfully goes to Select Case "Yes", then goes to the Goto Label100 line, but then jumps to the End If at the end of the block.

Why does the code just not jump 3 lines down to the Label and continue to the next ElseIf?

I have repoduced the code in a separate module as shown below and it works fine

VBA Code:
Sub Junk()
Dim SetWeightIgnore As String
SetWeightIgnore = "Yes"
            Select Case SetWeightIgnore
                Case "No"
                    Call ResultsN
                    Call RecordError
                    Call Skip1
                    GoTo SubEnd
                Case "Yes"
                    GoTo Label100
               End Select

Label100:
                MsgBox "Hello"

SubEnd:
End Sub

Ultimately, the aim here is that if the SetWeightIgnore variable = Yes, then the code should exit this ElseIf and move to the next ElseIf.

So I am really at a loss here as to why the Goto Label100 (as an example) in my model jumps all the way to the end of the If Block. :(

Cheers


On separate note, but related issue to get rid of the GoTo's, how can I terminate a procedure but also keep a userform active? Hence the Goto SubEnd above, becuse puting the code after SubEnd label in the main procedure into a separate sub, means that I had to put an End statement in that SubEnd Subroutine, which then unloads the userform :(.
 
Last edited:
Upvote 0
It's impossible to comment why code that you haven't posted is being affected. It is almost certainly due to something you have in the rest of the code. (jumping in and out of code blocks using goto will cause exactly what you describe)

Much like Goto, there are almost no situations where End on its own is necessary in well structured code. Usually something simple like converting a sub to a function that returns a value for success or failure will suffice.
 
Upvote 0
Hi Rory,

I have just changed the Goto Label 100, to MSGBOX "Hello", the code showed the msg no issue, but the next step was again to jump to then end of the If Block. Is there are built in reason why the code wont just move to thenext ElseIf ?
 
Upvote 0
Please understand that I am not jumping in and out of blocks of code with the Goto statement. I took your advice and have done as you have asked. I will have to do more homework, I dont have sufficient knowledge to change setting a variable in one routine and passing it back to another routine to be determined by a function. :(
 
Upvote 0
Is there are built in reason why the code wont just move to thenext ElseIf ?
I don't know - you haven't posted the part of the code that is being affected.
 
Upvote 0
I have just created a simple Yes/No function to return a True / False result with

VBA Code:
Function YesNo(SetWeightIgnore As String) As Boolean
If SetWeightIgnore = "Yes" Then
    SetWeightIgnoreResult = True
    Else
    SetWeightIgnoreResult = False
End If
End Function

so now the offending code in the primary If block is: The result from the SetWeightCalcs is that SetWeightIgnore = Yes, hence a the Function returns a True value

VBA Code:
            Call SetWeightCalcs

            YesNo (SetWeightIgnore)
           
            MsgBox SetWeightIgnoreResult

            Select Case SetWeightIgnoreResult
                Case False
                    Call ResultsN
                    Call RecordError
                    Call Skip1
                    GoTo SubEnd
                Case True
                    'Just keep moving on
             End Select


works fine in the above Case Select code, until the End Select is met, then the code jumps to then End of the primary IF block :(
 
Last edited:
Upvote 0
You still haven't posted the actual If block. Until you do, I cannot really comment. If this code is in part of your If block, then naturally you must have hit a true condition in the If clause to run it and no further clauses will be checked, since that is how If clauses work.
 
Last edited:
Upvote 0
In your original post the commented out "End If" is inside your select statement ie before End Select.
If the "If" statement is before Select the End If should be after End Select.
 
Upvote 0

Forum statistics

Threads
1,215,429
Messages
6,124,840
Members
449,193
Latest member
MikeVol

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