View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Laurie Laurie is offline
external usenet poster
 
Posts: 37
Default goto statement in VBA

First thanks both Rick and Tom for your confirmation, and Tom for your
demonstration!!!

To answer Rick's question, the coding is about a UDF for a very complicated
trading logic.
The main stream is already very long and it would be much cleaner and easier
to read if using goto statement to jump to the sub trading logic.

It's comforting to know under the label line, there can be as many
statements as necessary.

I guess my next question would be how to define the end of the block of
statements under the label line?

For the trading logic instance as demonstrated below, ideally, I would like
to retrieve the results from executing the statements under the label line to
be used in the statements following the "goto Label" line. Is this the case
by the code logic?

Also, after the block of statements, which are supposed to be under the
Label line, I would like to have another block of statements, like Select
Case statements, which are not belonging to the block of statements under the
Label line. Can this be done?

Can the VBA program automatically know how to handle the above two
situations or should I do something to help the VBA program recognize the
above two situations?

The UDF program flow is as below

Function Name1(...)

....(block of statements)...
if ... then
goto Label
... (more statements)...
end if

Label:
... (block of statements)...

Select Case Var1
.....
End Select

End Function




"Rick Rothstein (MVP - VB)" wrote:

There is no practical limit for how many statements can be in the block
following the GoTo statement. With that said, you will do yourself a big
favor if you learn not to use the GoTo statement (it makes code much harder
to read, especially several months from now when you come back to edit your
code). With the exception of On Error GoTo error handler, there is almost no
situation where GoTo is the correct solution. You can usually almost always
use an If-Then-Else or Select-Case block structure instead. Why don't you
outline what you are trying to code using GoTo and let's see if someone here
can show you a better way to code it.

Rick


"Laurie" wrote in message
...
Hi everyone who may help,

I am trying to use the goto statement in VBA in Excel.

From the Help, goto statement can jump to a label line.

I am wondering that, under the label line, which the goto statement jumps
to, how many statements are allowed?

In the help, only one line of statement is under the label line. Can there
be a block of statements under the label line?

Any help would be really appreciated.

Thanks a lot,
Laurie