Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA for Excel: "Who opened me?"

Status
Not open for further replies.

SB9999

Programmer
Apr 23, 2002
6
US
Is there a way in VBA for Excel that a worksheet can identify how it was opened?

For example if I have a macro in workbook Workbook1.xls and it calls a macro in another workbook Workbook2.xls, what code/property would I write/access in a macro in Workbook2.xls that would identify that it was Workbook1.xls that opened it?

In other words

Workbook1.xls contains macro test

Sub test()
Run "Workbook2.xls!test2"
end sub

Workbook2.xls contains macro test2

Sub test2()
MsgBox("This workbook was opened by:" & ActiveWorkbook.SOMEPROPERTY)
end sub

What is the SOMEPROPERTY that would produce a string value of "Workbook1.xls"?

Thanks in advance.
 
Hi, SB,

In each workbook that would be opening workbook2...
Code:
Sub test()
    Dim wbk As Workbook
    Set wbk = Application.ActiveWorkbook
    Workbooks.Open "Book2.xls"
    Application.Run "Book2.xls!test2", wbk
End Sub
In workbook2...
Code:
Sub test2(wbk As Workbook)
    MsgBox ("This workbook was opened by: " & wbk.Name)
End Sub
VOLA! :) Skip,
metzgsk@voughtaircraft.com
 
That's exactly what I was looking for - thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top