I have a workbook with the following code that runs when the workbook opens (code is in the ThisWorkbook module):
Private Sub Workbook_Open()
Sheet1.Activate
With Sheet3.ComboBox1
.Clear
.List = Application.Transpose(Range("AdminList"))
End With
With Sheet3.ComboBox2
.Clear
.List = Application.Transpose(Range("YesNo"))
End With
With Sheet3.ComboBox3
.Clear
.List = Application.Transpose(Range("YesNo"))
End With
With Sheet3.ComboBox4
.Clear
.List = Application.Transpose(Range("YesNo"))
End With
With Sheet3.ComboBox5
.Clear
.List = Application.Transpose(Range("Approval"))
End With
With Sheet3.ComboBox6
.Clear
.List = Application.Transpose(Range("YesNo"))
End With
With Sheet3.ComboBox7
.Clear
.List = Application.Transpose(Range("YesNo"))
.Visible = False
End With
With Sheet3.Label5
.Caption = ""
.Visible = False
End With
With Sheet4.Cells(1, 7)
.Value = 0
End With
Application.EnableEvents = True
End Sub
However, when I activate another worksheet, the code in the sub with the Worksheet_Activate event doesn't run:
Private Sub Worksheet_Activate()
TextBox1.Activate
End Sub
Even more confusing is that I have code that allows users to use the tab key to move through content controls. This is accomplished with KeyDown events. Here is the code for TextBox1:
Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If Shift = 1 And KeyCode = vbKeyTab Then
TextBox15.Activate
TextBox15.WordWrap = False
TextBox15.WordWrap = True
Else
If KeyCode = vbKeyTab Then
Application.EnableEvents = True
TextBox2.Activate
TextBox2.WordWrap = False
TextBox2.WordWrap = True
End If
End If
End Sub
If I manually click in TextBox1 and then hit tab to move to TextBox2, the KeyDown event works. Because of the Application.EnableEvents = True in the second half of the sub, now all other events work fine include the Worksheet_Activate event (I can click to another sheet and then back to the sheet in question, TextBox1 is activated).
My question is why doesn't the Worksheet_Activate event work while the KeyDown event does? Incidentally, Textbox.LostFocus events don't work either until the Textbox1_KeyDown event is run. That is initially how I knew there was an event problem. I have another Textbox that has validation code that runs when the textbox loses focus. If I click in this box before tabbing out of Textbox1, the data validation code doesn't work (LostFocus event doesn't even fire).
What am I missing?