View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Bob Phillips Bob Phillips is offline
external usenet poster
 
Posts: 1,726
Default Pass the variable into the sub function

You can simulate a control array like so.

First, add a class module, name it clsFormEvents, and insert this code

Option Explicit

Public WithEvents ctlTB As MSForms.TextBox

Private Sub ctlTB_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal
Shift As Integer)
If KeyCode = 13 Or KeyCode = 9 Then 'Enter or Tab
MsgBox ctlTB.Name
End If
End Sub

Then in your userform add this code

Dim colTB As Collection

Private Sub UserForm_Initialize()
Dim ctl As Control
Dim cEvents As clsFormEvents
'Go through the checkboxes and add them to the frame
Set colTB = New Collection
For Each ctl In Me.Controls
If TypeName(ctl) = "TextBox" Then
Set cEvents = New clsFormEvents
Set cEvents.ctlTB = ctl
colTB.Add cEvents
End If
Next ctl
End Sub

Unfortunately, this technique does not give you a textbox AfterUpdate, or
even Exit event. What I have done is to test the KeyDown event and check for
Enter or Tab and then display the name of te textbox being worked in.

Play with it and see if you can use it.

--
---
HTH

Bob

(change the xxxx to gmail if mailing direct)


"Richard" wrote in message
...
Hello,

Hope some can help me here.

As you can see the example below, I want to write the Sub function only
once
and able to run the sub function based which txtbox has been updated by a
user.

Assume X is the variable of the text box.

Can you help me to update "Private Sub txtbox"X"_AfterUpdate()" code,
please?

Thank you for your help and time.

Example I have 10 text boxes, txtbox1, txtbox2, txtbox3,.....txtbox9,
and
txtbox10.

Private Sub txtbox"X"_AfterUpdate()

intLnumber = X ' I want to run some coding here based which txtbox has
been updated.

End Sub