Question on VBA

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

This is a partial copy of my VBA macro. My question is: Is possible in VBA
to execute a sub in an " If ... then statement?"

If TopCell Is Nothing Then
MsgBox myStr(iCtr) & " wasn't found"
Exit Sub --- here can I request the execution of sub instead of
Exit sub?
Else
.Range(TopCell, BotCell).Select
End If
Next iCtr
End With
Thanks,
 
Hi
you can call another sub. Just insert it instead of exit sub
 
One way:

With something
For iCtr = 1 to n
If TopCell Is Nothing Then
MsgBox myStr(iCtr) & " wasn't found"
MySub
Else
.Range(TopCell, BotCell).Select
End If
Next iCtr
End With
'...
End Sub


Public Sub MySub()
'Do something here
End Sub
 
Why didn't you just try it, you would have found out very easily.

--

HTH

RP
(remove nothere from the email address if mailing direct)
 
Yes, simply call the sub.

Keep in mind that once the newly called sub finishes executing, code
execution *returns* to the current sub, on the line following where it was
called.

You only provided partial code but it seems you are within a loop.

If you call NewSub IN PLACE of your ExitSub, the loop will continue after
NewSub finishes.

If you call NewSub BEFORE your ExitSub, then ExitSub will be executed as
soon as NewSub sub finishes, so you will exit the loop (and the whole sub).

HTH,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top