From the following code, since it will create multiple layers of Sub. When I try "Exit Sub", it just exit the current Sub layer, but return to the previous Sub layer. So, my question is how to exit the code ENTIRELY when a condition "BCDEA" is met?
Actually, I know that I can run the entire code first, then search "BCDEA" in the worksheet, and manually delete the rest. However, it may lead to a huge computational cost which discourages me to do so.
Sub ABCDE()
Dim x, y As Variant
y = "ABCDE"
x = ""
Call GetPermutation(x, y)
End Sub
Sub GetPermutation(ByVal x, ByVal y)
Dim i, j, CurrentRow As Integer
CurrentRow = Application.WorksheetFunction.CountA(Columns(1))
j = Len(y)
If j < 2 Then
Cells(CurrentRow + 1, 1) = x & y
If Cells(CurrentRow + 1, 1) = "BCDEA" then Exit Sub
Else
For i = 1 To j
Call GetPermutation(x + Mid(y, i, 1), Left(y, i - 1) + Right(y, j - i))
Next
End If
End Sub