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

JeffGrant

Well-known Member
Joined
Apr 7, 2021
Messages
519
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
 
If your logic is very much to do with controlling a state machine about the status bets on four legged animals then you might be interested in this very simple statemachine that I developed for somebody else. It is all controlled from one worksheet and very simple worksheet change routine. The point of the software is that it is generic all the logic is controlled by what is written on the worksheet. States are change by typing TRUE into any of the yellow cells. starting from dormant the only one that changes state is to type into B3 to turn it on. here is the code:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
colno = Target.Column
If colno = 2 Then
rowno = Target.Row
If rowno > 2 And rowno < 11 Then
currentstate = Cells(15, 4)
trigger = Cells(rowno, 1)
rules = Range(Cells(1, 5), Cells(12, 7))
For i = 2 To 12
If rules(i, 2) = trigger And rules(i, 1) = currentstate Then
currentstate = rules(i, 3)
Cells(15, 4) = currentstate
Cells(rowno, colno) = False
Exit For
End If
Next i
End If
End If
End Sub
here is the worksheet
I have just noticed that B5 is TRUE I suggest typing FALSE in there to start with


State Machine.xlsm
ABCDEFG
1State Diagram Boxes and linesRules
2Triggersequated to AddressStatesInitial StateTriggerFinal State
3Turn OnFALSEDormantDormantTurn OnRunning
4Turn OffFALSERunningRunningTurn OffDormant
5Trigger LayTRUELay TriggeredRunningTrigger LayLay Triggered
6Trigger BackFALSEBack TriggeredRunningTrigger BackBack Triggered
7Bet ConfirmedFALSELay PlacedLay TriggeredBet ConfirmedLay placed
8ErrorFALSEBack PlacedBack TriggeredBet ConfirmedBack Placed
9Trigger Lay CloseFALSELay Close TriggeredLay TriggeredErrorRunning
10Trigger Back CloseFALSEBack Close TriggeredBack TriggeredErrorRunning
11Lay placedTrigger Lay CloseRunning
12Back PlacedTrigger Back CloseRunning
13
14
15Current StateDormant
Sheet1
 
Upvote 0

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Hi Guys,
I have resolved the issue by going back a couple of versions, and rethinking the nested if structure. All seems to be working fine. I just needed a couple of days away from this project to rehash it my mind.
However, offthelip, you have introduced a new way of thinking that could well be a much more efficient structure. I will defintely spend some time investigating this strategy.
Thansk to all.
 
Upvote 0

Forum statistics

Threads
1,215,412
Messages
6,124,761
Members
449,187
Latest member
hermansoa

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