Aim: To assign a keydown event to 4 TextBoxes on Worksheet (Sheet1) via Class Module
I have an Excel Worksheet (Sheet1) that has 4 TextBoxes on it (default names - TextBox1, TextBox2, TextBox3, TextBox4).
Steps Taken:
(1) Created a class module named ClsEventTxtBx with following codes.
Public WithEvents CTxtBx As MSForms.TextBox
Private Sub CTxtBx_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = vbKeyTab Then Debug.Print CTxtBx.Name
End Sub
(2) Added the following codes in the Workbook Open event.
Dim TxtBxArr() As New ClsEventTxtBx
Private Sub Workbook_Open()
Dim i As Integer, shp As Shape
For Each shp In Sheet1.Shapes
If shp.Type = msoOLEControlObject Then
If TypeName(shp.OLEFormat.Object.Object) = "TextBox" Then
i = i + 1
ReDim TxtBxArr(1 To i)
Set TxtBxArr(i).CTxtBx = shp.OLEFormat.Object.Object
End If
End If
Next shp
End Sub
Could you please find what is wrong with the code?
Redim Preserveif you want to maintain the existing array content. - Tim Williams