Is there a better alternative to multiple Goto Line labels?

JeffGrant

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

I have a long module that tests a lot of conditions in the format of:
VBA Code:
If Then
   elseif
   elseif
   elseif
   else:
end if


and so on.

To further fine tune the model I need to incorporate nested ifs.
I already have many nested ifs and that is not a problem.
However, what I would like to do is introduce a structure like

VBA Code:
If Then
   elseif
   elseif
      if then
          jump to the next elseif outside of this If block.
      elseif
      end if
   elseif
   elseif
end if


I could achieve this by doing something like:

VBA Code:
If Then
   elseif
   elseif
      if then
         Goto Label_1
      elseif
      end if
Label_1:
   elseif
      if then
         GoTo Label_2
      elseif
      end if
Label_2:
elseif
elseif
elseif
end if


That would mean that I would have a lot of Goto's and Labels. Is there a better alternative to using so much line jumping?
I am concerned that this is a really poor way to code this module.

Thanks for your suggestions
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
It is a really poor way to code as you suggest. You will find that it becomes increasing difficult as well.

How about using a combination of If and IfElse and Select Case and avoid using GoTo and labels and call sub routines.

I only use labels for error trapping.
 
Upvote 0
Difficult to answer when we don't know the purpose better, but would it be suitable for your use, for example:
1. Select Case
2. Change goto to function call.
3. Turn the test into a negation (if not)?

If NOT is just an if statement, but sometimes it can change the structure surprisingly much.
 
Upvote 0
By the looks of it, you need to refactor your code and quite possibly redesign your workbook.
 
Upvote 0
Thank you all foryour contributions.

I was thinking about this overnight and the mostly solution will be using the IF NOT solution because of te structral change it will make. So it was really goo to see that offerred up as a solution this morning.

Having said that, Rory, you are abolsutly correct, I have thought about a refactoring solution for a considerable time now, it is just my lackof skills that is holding me back I think :(
 
Upvote 0

Forum statistics

Threads
1,215,341
Messages
6,124,391
Members
449,155
Latest member
ravioli44

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