2017-08-19 3 views
0

Je souhaite appeler une sous-routine provenant de la même sous-routine en fonction d'une condition, mais en sortir dès qu'elle est remplieComment appeler un sous-marin dans le même sous-marin une fois?

Dim p as Long 

Sub Main() 
    Debug.Print "Start Main" 
    p = 1 
    call A 
    Debug.Print "End Main" 
End Sub 

Sub A() 

    Debug.Print "Start" 

    if p = 1 then 
    p = 2 
    call A 
    End if 

    Debug.Print "End" 

End Sub 

Si je cours Main je vais obtenir la sortie suivante ce qui est normal puisque nous appelons A deux fois pour deux start et deux extrémités

Start Main 
Start 
Start 
End 
End 
End Main 

Mais je ne veux pas revenir à la End if de sous une fois terminée, cette sous première fois ce qui signifie que je veux la sortie suivante si je l'appelle principal

Start Main 
Start 
Start 
End 
End Main 

avis il n'y a qu'un seul End

+2

Juste '' après la sortie Sub' appel A', ou ajouter un 'Else' à votre bloc 'If'. –

Répondre

1

Il suffit d'ajouter un Exit Sub où vous voulez A() pour mettre fin à

Dim p as Long 

Sub Main() 
    Debug.Print "Start Main" 
    p = 1 
    call A 
    Debug.Print "End Main" 
End Sub 

Sub A() 

    Debug.Print "Start" 

    if p = 1 then 
    p = 2 
    call A 
    Exit Sub 
    End if 

    Debug.Print "End" 

End Sub 
1

utiliser la clause else

Dim p As Long 

Sub Main() 

    Debug.Print "Start Main" 

    p = 1 
    Call A 

    Debug.Print "End Main" 

End Sub 

Sub A() 

    Debug.Print "Start" 

    If p = 1 Then 
     p = 2 
     Call A 
    Else 
     Debug.Print "End" 
    End If 

End Sub