ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Tell whether or not Sub B was called by Sub A? (https://www.excelbanter.com/excel-programming/425003-tell-whether-not-sub-b-called-sub.html)

IanKR

Tell whether or not Sub B was called by Sub A?
 
For example, if I have two Subs in my project, say A and B, and B may
sometimes be called from within A and at other times it may be run
independently of A, other than setting a global Boolean as illustrated
below, is there a slick way of telling?

Dim SubBCalledFromSubA as Boolean

Sub A()
'first reset Boolean, just in case
SubBCalledFromSubA = FALSE

.....

SubBCalledFromSubA = TRUE
Call B
....

'reset Boolean
SubBCalledFromSubA = FALSE
End Sub

Sub B()
If SubBCalledFromSubA = TRUE Then
'code here
End If

If SubBCalledFromSubA = FALSE Then
'some other code here
End If
End Sub

I thought that Application.Caller (or something similar) might be relevant
here, but it doesn't appear to be, judging by the help file.

Thanks

Ian


Gary Brown[_5_]

Tell whether or not Sub B was called by Sub A?
 
'/-----------------------------------/
Sub A()
Dim SubBCalledFromSubA As Boolean

'first reset Boolean, just in case
SubBCalledFromSubA = False

' .....

SubBCalledFromSubA = True
Call B(SubBCalledFromSubA)
' ....

'reset Boolean
SubBCalledFromSubA = False
End Sub

'/-----------------------------------/
Sub B(Optional blnCheck)
If IsMissing(blnCheck) Then blnCheck = True

If blnCheck = True Then
'code here
MsgBox "It's True!"
End If

If blnCheck = False Then
'some other code here
MsgBox "It's False :O<"
End If
End Sub
'/-----------------------------------/


--
Hope this helps.
If it does, please click the Yes button.
Thanks in advance for your feedback.
Gary Brown



"IanKR" wrote:

For example, if I have two Subs in my project, say A and B, and B may
sometimes be called from within A and at other times it may be run
independently of A, other than setting a global Boolean as illustrated
below, is there a slick way of telling?

Dim SubBCalledFromSubA as Boolean

Sub A()
'first reset Boolean, just in case
SubBCalledFromSubA = FALSE

.....

SubBCalledFromSubA = TRUE
Call B
....

'reset Boolean
SubBCalledFromSubA = FALSE
End Sub

Sub B()
If SubBCalledFromSubA = TRUE Then
'code here
End If

If SubBCalledFromSubA = FALSE Then
'some other code here
End If
End Sub

I thought that Application.Caller (or something similar) might be relevant
here, but it doesn't appear to be, judging by the help file.

Thanks

Ian



PA

Tell whether or not Sub B was called by Sub A?
 
May be you could have an argument to sub B:

sub B(byval sCallerProc as string)
if sCallerProc=A then
.....
else
.....
endif
end sub

that won't work if B is called directly by the user. It will need to fire
sub C that will fire sub B.

I'm not sure if I'm clear.

PA


"IanKR" wrote:

For example, if I have two Subs in my project, say A and B, and B may
sometimes be called from within A and at other times it may be run
independently of A, other than setting a global Boolean as illustrated
below, is there a slick way of telling?

Dim SubBCalledFromSubA as Boolean

Sub A()
'first reset Boolean, just in case
SubBCalledFromSubA = FALSE

.....

SubBCalledFromSubA = TRUE
Call B
....

'reset Boolean
SubBCalledFromSubA = FALSE
End Sub

Sub B()
If SubBCalledFromSubA = TRUE Then
'code here
End If

If SubBCalledFromSubA = FALSE Then
'some other code here
End If
End Sub

I thought that Application.Caller (or something similar) might be relevant
here, but it doesn't appear to be, judging by the help file.

Thanks

Ian



IanKR

Tell whether or not Sub B was called by Sub A?
 
'/-----------------------------------/
Sub A()
Dim SubBCalledFromSubA As Boolean

'first reset Boolean, just in case
SubBCalledFromSubA = False

' .....

SubBCalledFromSubA = True
Call B(SubBCalledFromSubA)
' ....

'reset Boolean
SubBCalledFromSubA = False
End Sub

'/-----------------------------------/
Sub B(Optional blnCheck)
If IsMissing(blnCheck) Then blnCheck = True

If blnCheck = True Then
'code here
MsgBox "It's True!"
End If

If blnCheck = False Then
'some other code here
MsgBox "It's False :O<"
End If
End Sub
'/-----------------------------------/

--
Hope this helps.
If it does, please click the Yes button.
Thanks in advance for your feedback.
Gary Brown


Many thanks, Gary - "Yes"!

IanKR

Tell whether or not Sub B was called by Sub A?
 
May be you could have an argument to sub B:

sub B(byval sCallerProc as string)
if sCallerProc=A then
....
else
....
endif
end sub

that won't work if B is called directly by the user. It will need to fire
sub C that will fire sub B.

I'm not sure if I'm clear.


I didn't follow the Sub C bit, I'm afraid. Gary's reply covers it, with the
optional argument.


PA

Tell whether or not Sub B was called by Sub A?
 
Gary's solution is perfect if you don't want to call proc B directly (e.g By
a button) because you cannot call a proc with an argument directly.

If you would have top call b directly, you would need to call C that would
call B.

I'm not sure if this is clearer but bottom line, if gary's solution works
for you, go for it !

pa

"IanKR" wrote:

May be you could have an argument to sub B:

sub B(byval sCallerProc as string)
if sCallerProc=A then
....
else
....
endif
end sub

that won't work if B is called directly by the user. It will need to fire
sub C that will fire sub B.

I'm not sure if I'm clear.


I didn't follow the Sub C bit, I'm afraid. Gary's reply covers it, with the
optional argument.



IanKR

Tell whether or not Sub B was called by Sub A?
 
Gary's solution is perfect if you don't want to call proc B directly (e.g
By
a button) because you cannot call a proc with an argument directly.

If you would have top call b directly, you would need to call C that would
call B.

I'm not sure if this is clearer but bottom line, if gary's solution works
for you, go for it !


Yes- it'll work, because these Subs are always called from other Subs (or
from code associated with either Contol Toolbox Buttons or userform
buttons - there's a lot of 'nested' procedures in the project), and never
directly by the user. Sorry - perhaps I should have mentioned that in my
original post.



All times are GMT +1. The time now is 04:24 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com