private sub- which procedure called it

S

Sunil

When i run a macro that changes a cell the code below is run.
What i need to know is which procedure caused the code below to be called.


Private Sub Worksheet_SelectionChange(ByVal Target As Range)

which macro procedure has called this routine ?

.....run code depending on which procedure has called this sub routine

End sub
 
C

Chip Pearson

There is no built-in way to determine what procedure called
another. The only way would be to create a public variable, put
the caller's name in that variable, and read that variable in the
routine.

[in module1]
Public WhoCalled As New Collection

Sub AAA()
WhoCalled.Add "AAA"
' more code
End Sub

[in ThisWorkbook]
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim CalledBy As String
CalledBy = WhoCalled(WhoCalled.Count)
Select Case CalledBy
Case "AAA"
' do something
Case "BBB"
' do something else
Case ....
End Select

End Sub


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 
C

Chip Pearson

Small revision.....

Change

CalledBy = WhoCalled(WhoCalled.Count)
' to
CalledBy = WhoCalled(WhoCalled.Count)
WhoCalled.Remove WhoCalled.Count


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com


Chip Pearson said:
There is no built-in way to determine what procedure called
another. The only way would be to create a public variable,
put the caller's name in that variable, and read that variable
in the routine.

[in module1]
Public WhoCalled As New Collection

Sub AAA()
WhoCalled.Add "AAA"
' more code
End Sub

[in ThisWorkbook]
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim CalledBy As String
CalledBy = WhoCalled(WhoCalled.Count)
Select Case CalledBy
Case "AAA"
' do something
Case "BBB"
' do something else
Case ....
End Select

End Sub


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com


Sunil said:
When i run a macro that changes a cell the code below is run.
What i need to know is which procedure caused the code below
to be called.


Private Sub Worksheet_SelectionChange(ByVal Target As Range)

which macro procedure has called this routine ?

....run code depending on which procedure has called this sub
routine

End sub
 

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

Top