Results 1 to 9 of 9
  1. #1
    Gold Lounger
    Join Date
    Feb 2004
    Location
    Cape Town, RSA
    Posts
    3,444
    Thanks
    0
    Thanked 1 Time in 1 Post

    Exit Sub (Excel2000>)

    Hi,

    You can group macro's into a macro sothat they all run : eg:
    Sub Project()
    Macro1
    Macro2
    Macro3
    End sub

    But what can I do if I want to exit the entire group if a condition is reached in macro1.

    Something like :
    If Condition_Met = true then
    Exit Project
    end if

    ???
    Regards,
    Rudi

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Exit Sub (Excel2000>)

    You can change the macros into functions that return a Boolean value - True to continue, False to stop.

    Function Macro1() As Boolean
    Macro1 = True ' default
    ...
    If <somecondition> Then
    Macro1 = False
    Exit Function
    End If
    ...
    End Function

    etc.

    Sub Projectt()
    If Macro1 = True Then
    If Macro2 = True Then
    Macro3
    End If
    End If
    End Sub

    You could also use a global variable.

  3. #3
    Gold Lounger
    Join Date
    Feb 2004
    Location
    Cape Town, RSA
    Posts
    3,444
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Exit Sub (Excel2000>)

    If I change the macro into a function macro, can it still perform actions on the sheet or workbook. Functions only return a value to the sheet???
    Sorry for my ignorance here!

    Say macro1 is importing a text file. If the file is not available it must exit the entire project too!
    Regards,
    Rudi

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Exit Sub (Excel2000>)

    If you use a function as a user-defined worksheet function i.e. in a cell formula, it cannot modify the workbook except returning a value in the cell. If you call a function in VBA code, however, it can modify the workbook any way you want

  5. #5
    Gold Lounger
    Join Date
    Feb 2004
    Location
    Cape Town, RSA
    Posts
    3,444
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Exit Sub (Excel2000>)

    Thanx - that makes sense!
    Regards,
    Rudi

  6. #6
    Gold Lounger
    Join Date
    Feb 2004
    Location
    Cape Town, RSA
    Posts
    3,444
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Exit Sub (Excel2000>)

    Hi Hans,

    Is this the way to go if I use a global variable! Any advice/guidance. Tx

    Option Explicit
    Public QuitProject As Boolean

    Sub Project()
    QuitProject = True
    Macro1
    If QuitProject = False Then
    Exit Sub
    Else
    Macro2
    Macro3
    End If
    End Sub

    Sub Macro1()
    On Error GoTo EH
    Workbooks.Open Filename:="C:Test.txt"
    MsgBox "Macro 1 running!"
    Exit Sub
    EH:
    QuitProject = False
    End Sub

    Sub Macro2()
    MsgBox "Macro 2 running!"
    End Sub

    Sub Macro3()
    MsgBox "Macro 3 running!"
    End Sub
    Regards,
    Rudi

  7. #7
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Exit Sub (Excel2000>)

    Public QuitProject As Boolean

    Sub Macro1() As Boolean
    ...
    If <somecondition> Then
    QuitProject = True
    Exit Sub
    End If
    ...
    End Sub

    etc.

    Sub Projectt()
    QuitProject = False
    Macro1
    If QuitProject = True Then Exit Sub
    Macro2
    If QuitProject = True Then Exit Sub
    Macro3
    End Sub

  8. #8
    Gold Lounger
    Join Date
    Feb 2004
    Location
    Cape Town, RSA
    Posts
    3,444
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Exit Sub (Excel2000>)

    Ok...thanx. This method is more flexible, and can be added to any macro to exit the sub if required. I assume that if I only want to exit if a condition is met in Macro1, then I don't need the If QuitProject statement ater Macro2 and Macro3. (as below) ... Thanx
    Macro2
    If QuitProject = True Then Exit Sub
    Macro3
    End Sub
    Regards,
    Rudi

  9. #9
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Exit Sub (Excel2000>)

    You only need to test QuitProject where you need it. <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •