hi Its quite involved,
userform class RaiseEvent OnEnter control
userform class event code from Jaafar Tribak RaiseEvent on enter or exit of control.
This is especially useful when using multiple cascading combobox list which are dependant on each others selection
combobox's don't have this feature
Userform code
Option Explicit
Public Event OnEnter(ctrl As msforms.Control)
Public Event OnExit(ctrl As msforms.Control)
Private oXitClass As CtlExitCls
Private bFormUnloaded As Boolean
Private oPrevActiveCtl As msforms.Control
Private oCol As New Collection
Sub Combobox_List(myCtrl As Control)
Dim myRng As Range, objCtrl As Control
Set myRng = ThisWorkbook.Names("petSkill").RefersToRange
Dim e, v
With myRng
v = .Value
End With
With CreateObject("scripting.dictionary")
.comparemode = 1
For Each e In v
If Not .exists(e) Then .Add e, Nothing
Next e
For Each objCtrl In Me.Controls
If TypeName(objCtrl) Like "ComboBox" And Not objCtrl.Name Like myCtrl.Name And .exists(objCtrl.Value) Then
.Remove (objCtrl.Value)
End If
Next
If .Count Then
myCtrl.List = Application.Transpose(.keys)
End If
End With
End Sub
Private Sub CommandButton1_Click()
Unload Me
End Sub
Private Sub UserForm_Layout()
Call WatchEvents
End Sub
Private Sub UserForm_Terminate()
Call CleanUp
End Sub
Private Sub WatchEvents()
If Not oXitClass Is Nothing Then Exit Sub
Set oXitClass = New CtlExitCls
Set oXitClass.FormCtrl = Me
bFormUnloaded = False
Set oPrevActiveCtl = Me.ActiveControl
RaiseEvent OnEnter(Me.ActiveControl)
Do While bFormUnloaded = False
If Not oPrevActiveCtl Is Nothing Then
If Not oPrevActiveCtl Is Me.ActiveControl Then
RaiseEvent OnExit(oPrevActiveCtl)
RaiseEvent OnEnter(Me.ActiveControl)
Me.ActiveControl.SetFocus
End If
End If
Set oPrevActiveCtl = Me.ActiveControl
DoEvents
Loop
End Sub
Private Sub CleanUp()
bFormUnloaded = True
RaiseEvent OnExit(oPrevActiveCtl)
Set oXitClass = Nothing
Set oCol = Nothing
Set oPrevActiveCtl = Nothing
End Sub
Class Module named CtlExitCls
Option Explicit
Public WithEvents FormCtrl As UserForm1
Private Sub FormCtrl_OnEnter(ctrl As msforms.Control)
' MsgBox "You Exited the Control : " & "(" & Ctrl.Name & ")"
Select Case True
Case TypeName(ctrl) Like "ComboBox"
Call FormCtrl.Combobox_List(ctrl)
Case TypeName(ctrl) Like "TextBox"
' MsgBox "You Entered the Control : " & "(" & Ctrl.Name & ")"
Case TypeName(ctrl) Like "CommandButton"
' MsgBox "You Entered the Control : " & "(" & Ctrl.Name & ")"
End Select
End Sub
Private Sub FormCtrl_OnExit(ctrl As msforms.Control)
' MsgBox "You Exited the Control : " & "(" & Ctrl.Name & ")"
Select Case True
Case TypeName(ctrl) Like "ComboBox"
' MsgBox "You Exited the ComboBox Control : " & "(" & Ctrl.Name & ")"
Case TypeName(ctrl) Like "TextBox"
' MsgBox "You Exited the TextBox Control : " & "(" & Ctrl.Name & ")"
Case TypeName(ctrl) Like "CommandButton"
' MsgBox "You Exited the CommandButton Control : " & "(" & Ctrl.Name & ")"
End Select
End Sub