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:
 
Thank you all for your contributions. After steping back and thinking about this a most obvious coding error have been applied on bulk. Yep Rory, you were on the money mate.

Within the ElseIf blocks, I had coded some more nested Ifs. So what was happening is that as the nested If's were passing/failing and the code was backing out, then ultimately the ElseIf that was triggered in the first place, is now considered and being met and so the next step was to jump to the EndIf.

So the problem has been identified but now I need to rethink how to achieve what the NestIfs set out to do. :)

I nned to think through how this can be achieved using Functions to return a True/False value has Rory has suggested.

Thanks again for all your help.
 
Upvote 0

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand

Forum statistics

Threads
1,215,425
Messages
6,124,824
Members
449,190
Latest member
rscraig11

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