Hi RJSUK,
You can use a Class along with a holding object to capture SOME events.
You can not capture the Exit and Enter ones though.
This example will add the 81 textboxes, as per your code.
It will also output the textbox name and contents to the immediate window (CTRL+G in VBE)
Once the userform is displayed click it to add the textboxes.
Now type ABC in any of the boxes.
It should change the background colour to red. Now move to another textbox.
The previous one should return to white and the new one bacomes red.
'---Userform1 -------Userform Module------
Option Explicit
Private m_strLastName As String
Private ArrayTxt(81) As clsTxt
Public WithEvents EventTxt As MSForms.TextBox
Private Sub EventTxt_Change()
If m_strLastName <> EventTxt.Name And m_strLastName <> "" Then
Me.Controls(m_strLastName).BackColor = QBColor(15)
End If
m_strLastName = EventTxt.Name
Me.Controls(m_strLastName).BackColor = QBColor(12)
Debug.Print EventTxt.Name, EventTxt.Text
End Sub
Private Sub UserForm_Click()
Dim Counter_Time
Dim Counter_Lane
Dim LaneBox
Dim strName As String
Dim intIndex As Integer
For Counter_Time = 0 To 26
For Counter_Lane = 0 To 2
intIndex = intIndex + 1
strName = Format(Counter_Time, "00") & Format(Counter_Lane, "0")
Set LaneBox = Controls.Add("Forms.TextBox.1")
Set ArrayTxt(intIndex) = New clsTxt
Set ArrayTxt(intIndex).MyTxt = LaneBox
With ArrayTxt(intIndex)
With .MyTxt
.Name = strName
.BorderStyle = 1
.Left = 36 + (Counter_Lane * 162)
.Top = 10 + ((Counter_Time - 3) * 15.25)
.Width = 156
.Height = 15
End With
End With
Next Counter_Lane
Next Counter_Time
End Sub
'--------------------------------------
'----clsTxt-----------Class Module--
Option Explicit
Public LaneIndex
Public TimeIndex
Public WithEvents MyTxt As MSForms.TextBox
Private Sub MyTxt_Change()
Set UserForm1.EventTxt = Me.MyTxt
End Sub
'-------------------
Depending on exactly what you are doing this technique may be of use.
Cheers
Andy
Excel
Video Tutorials / Excel Dashboards Reports
Bookmarks