Option Explicit
' in userform's code module
Dim LInkedBoxes As Collection
Public WithEvents ActiveBox As clsLinkedTextBox
Private Sub ActiveBox_Change()
With ActiveBox
MsgBox .Name & ", linked to " & .LinkedCell.Address(, , , True) & ", has changed" & " to """ & .Text & """"
End With
End Sub
Private Sub UserForm_Click()
Sheet1.Range("a1").Value = "hellow"
Call makeChange
End Sub
Private Sub UserForm_Initialize()
Dim aLinkedBox As clsLinkedTextBox
Dim RangeToLink As Range, oneCell As Range
Dim nextTop As Single
Set LInkedBoxes = New Collection
Set RangeToLink = Range("A1:A10")
nextTop = 5
For Each oneCell In RangeToLink
Set aLinkedBox = New clsLinkedTextBox
With aLinkedBox
.LinkToCell LinkCell:=oneCell, Parent:=Me
.Height = 22: .Width = 75
.Top = nextTop
.Left = 5
.Font.Size = 12
.ForeColor = RGB(255, 0, 0)
nextTop = nextTop + .Height + 4
End With
LInkedBoxes.Add Item:=aLinkedBox
Next oneCell
Set aLinkedBox = Nothing
End Sub
Private Sub UserForm_Terminate()
Dim oneBox As clsLinkedTextBox
For Each oneBox In LInkedBoxes
Set oneBox = Nothing
Next oneBox
Set LInkedBoxes = Nothing
End Sub