To make this work it needs at least a public object variable of type of your class. And this object variable must be set to be a new instance of your class. This object variable then, and only this object variable, is the public accessable instance of your class.
Example:
Let your class be named clsWorkbook
and having following code:
Option Explicit
Private m_cell As Range
Private WithEvents m_wb As Workbook
Property Let cell(cellrange As Range)
Set m_cell = cellrange
End Property
Property Get cell() As Range
Set cell = m_cell
End Property
Public Property Let Workbook(wb As Workbook)
Set m_wb = wb
End Property
Public Property Get Workbook() As Workbook
Set Workbook = m_wb
End Property
Private Sub m_wb_SheetChange(ByVal Sh As Object, ByVal Target As Range) 'simplified, but accurate
Application.EnableEvents = False
For Each m_cell In Target
ReplaceTask.Show
Next m_cell
Application.EnableEvents = True
End Sub
Let your UserForm named ReplaceTask
having following code:
Option Explicit
Private Sub UserForm_Initialize()
Debug.Print oWB.Workbook.Name
Debug.Print oWB.cell.Address
End Sub
And in a default Module have following code:
Option Explicit
Public oWB As clsWorkbook
Public Sub test()
Set oWB = New clsWorkbook
oWB.Workbook = ThisWorkbook
End Sub
Now, after Sub test()
was run, do changig something in a worksheet in the workbook the code is in. This should trigger the Sub m_wb_SheetChange(ByVal Sh As Object, ByVal Target As Range)
of your class object oWB
then, which shows the user form which also can access oWB.Workbook.Name
and oWB.cell.Address
.
Because of the discussion about the need of a global instance of clsWorkbook
lets have a complete example which one can reconstruct and which shows how the clsWorkbook
can be a private class member:
Let your class be named clsWorkbook
and having following code:
Option Explicit
Private m_cell As Range
Private WithEvents m_wb As Workbook
Property Let Cell(cellrange As Range)
Set m_cell = cellrange
End Property
Property Get Cell() As Range
Set Cell = m_cell
End Property
Property Let Workbook(wb As Workbook)
Set m_wb = wb
End Property
Property Get Workbook() As Workbook
Set Workbook = m_wb
End Property
Private Sub m_wb_SheetChange(ByVal Sh As Object, ByVal Target As Range) 'simplified, but accurate
Application.EnableEvents = False
Dim frm As ReplaceTask
For Each m_cell In Target
Set frm = New ReplaceTask
frm.Init Me
frm.Show
Next m_cell
Application.EnableEvents = True
End Sub
Let your UserForm named ReplaceTask
having following code:
Option Explicit
Private m_ParentClass As clsWorkbook
Friend Sub Init(ByVal p As clsWorkbook)
Set m_ParentClass = p
Me.Caption = p.Workbook.Name & " : " & p.Cell.Address
End Sub
And in default class module ThisWorkbook
have following code:
Option Explicit
Private oWB As clsWorkbook
Private Sub Workbook_Open()
Set oWB = New clsWorkbook
oWB.Workbook = Workbooks.Open("P:/Mappe1.xlsx")
End Sub
Now the clsWorkbook
gets instantiated while workbook open and is a private member of ThisWorkbook
and it's workbook member is the workbook which was opened addditional. There the SheetChange
are listened by the clsWorkbook
oWB
instance.
And because the ReplaceTask
user form gets instantiated in clsWorkbook
and was given the class instance as parameter, this user form knows the class members too.