MWE,
If you want to get the VBProject of a CodePane, use
Set VBProj = VBCodePane.CodeModule.Parent.Collection.Parent
To get the Workbook associated with the CodePane, you have to
loop through the workbooks and compare file names. E.g.,
Dim WB As Workbook
Dim VBProj As VBIDE.VBProject
Dim VBComp As VBIDE.VBComponent
Set VBProj = VBCodePane.CodeModule.Parent.Collection.Parent
For Each WB In Application.Workbooks
If WB.FullName = VBProj.Filename Then
Exit For
End If
Next WB
Debug.Print WB.FullName
To get the worksheet associated with the CodePane, you have to
loop through the worksheets and compare code names. (The
following assumes that VBCodePane is set to the code pane of a
sheet module.)
Dim WS As Worksheet
For Each WS In WB.Worksheets
If WS.CodeName = VBCodePane.CodeModule.Parent.Name Then
Exit For
End If
Next WS
Debug.Print WS.Name
--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com