Skip a statment or line in vba

  • Thread starter Thread starter Cue
  • Start date Start date
C

Cue

Is it possible to skip a statement(s)/line(s) in vba?

Im using answer msg box and I want to skip or pass a section if the answer
is no.
 
You would use:

If condition Then
action_if_true
Else
action_if_false
Endif

rest_of_macro

Hope this helps.

Pete
 
Hi Pete_UK. Thanks for your response.

I'm not sure if that would work. I should be more detailed.

I want to skip several lines in a macro/vba. Using the answer msg box, if
the answer is no then I want it to go to several lines down in the vba to
perform more actions on the workseet. if it that is possible, here is part of
the code:

Answer = MsgBox("Do you want to delete this cell?", vbYesNo)
If Answer = vbYes Then Range("A2").Select
If Answer = vbNo Then GoTo LN 118

It would skip to this line in vba:

Answer = MsgBox("Do you want to close '" & ActiveWorkbook.Name & "'?",
vbYesNo)
If Answer = vbYes Then ActiveWindow.Close
If Answer = vbNo Then Exit Sub

Is this possible? If so, how?
 
There are several ways of doing it - here's one way:

Answer = MsgBox("Do you want to delete this cell?", _
vbYesNo)
If Answer = vbYes Then
Range("A2").Select
Else
Answer = MsgBox("Do you want to close '" & _
ActiveWorkbook.Name & "'?", vbYesNo)
If Answer = vbYes Then
ActiveWindow.Close
Else
Exit Sub
Endif
Endif

'rest of code, with A2 selected

If you want to use GoTo then set up a label and jump to that (look in
VBA Help).

Hope this helps.

Pete
 

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

Similar Threads


Back
Top