Adding Macro to TextBox in VBA

jackass209

New Member
Joined
Jul 14, 2007
Messages
49
I've got a userform which adds Textboxes automatically depending on a certain value. And the amount of Textboxes varies from 1 - 100. I add them with the line:

Set MyControl = UserForm1.Frame1.Controls.Add("forms.textbox.1", strControl, Visible)

Now my question is:
Can I add a macro to each of these textboxes automatically? A macro for the Change event I think it is.

All textboxes would have the same macro.

I kinda hoped it would be one of the following:
MyControl.OnAction = "test"
MyControl.Change = "test"

But you guessed it, that doesn't work!
Hope someone can help me out. Thanks!
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.

jackass209

New Member
Joined
Jul 14, 2007
Messages
49
Hi al_b_cnu,

Thanks for your reply. I don't see in that example how to add a macro to a textbox on a userform. That's all I need to know. Maybe I'm missing something but I don't see it.

Hope you or someone else can help me with this.

Thanks!
 
Upvote 0

al_b_cnu

Well-known Member
Joined
Jul 18, 2003
Messages
4,526
Hi,

Adapting Andrews code to suit your requirement follows. This code will catch the chenge event for all textboxes in the userform with a name starting 'Text':
Code:
'   Class module named CtrlClass

Public WithEvents CBGroup As MSForms.TextBox

Private Sub CBGroup_Change()
    MsgBox "TextBox " & CBGroup.Name & " changed"
End Sub

Code:
'    UserForm module

Dim CBs() As New CtrlClass

Private Sub UserForm_Initialize()
    Dim CBCount As Integer
    Dim Ctrl As Control
    CBCount = 0
    For Each Ctrl In UserForm1.Controls
        If TypeName(Ctrl) = "TextBox" Then
            If Left$(Ctrl.Name, 4) = "Text" Then
                CBCount = CBCount + 1
                ReDim Preserve CBs(1 To CBCount)
                Set CBs(CBCount).CBGroup = Ctrl
            End If
        End If
    Next Ctrl
End Sub
 
Upvote 0

jackass209

New Member
Joined
Jul 14, 2007
Messages
49
Oh okay... I see what you mean. I'll try to see if I can get this to work. I'll keep you posted! Thanks alot!
 
Upvote 0

jackass209

New Member
Joined
Jul 14, 2007
Messages
49
I get an error and it selects the following in blue:

ReDim Preserve CBs(1 To

I copied the codes you provided and put them in the userform.
Changed the Userform name.
It says something like "Variable not defined"

So I take it it's about the CBs but I'm not sure bout that.
Hope you can help me out... again.
Thanks!
 
Upvote 0

al_b_cnu

Well-known Member
Joined
Jul 18, 2003
Messages
4,526
Hi,

Did you declare the CBs variable at the top of the userform code,i.e. OUTSIDE any subs?
 
Upvote 0

jackass209

New Member
Joined
Jul 14, 2007
Messages
49
This is the code I have:

' Class module named CtrlClass

Public WithEvents CBGroup As MSForms.TextBox

Private Sub CBGroup_Change()
MsgBox "TextBox " & CBGroup.Name & " changed"
End Sub

' UserForm module

Dim CBs() As New CtrlClass

Private Sub UserForm_Initialize()
Dim CBCount As Integer
Dim Ctrl As Control
CBCount = 0
For Each Ctrl In RegRap.Frame1.Controls
If TypeName(Ctrl) = "TextBox" Then
If Left$(Ctrl.Name, 4) = "Text" Then
CBCount = CBCount + 1
ReDim Preserve CBs(1 To CBCount)
Set CBs(CBCount).CBGroup = Ctrl
End If
End If
Next Ctrl
End Sub

So only changed the UserForm1 into RegRap.Frame1.
Unless I gotta change some more?
 
Upvote 0

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,358
Office Version
  1. 365
Platform
  1. Windows
And is all this in the userform module?

I think what Alan means here is that the code should be going into a class module called CtrlClass.
Code:
'  Class module named CtrlClass 

Public WithEvents CBGroup As MSForms.TextBox 

Private Sub CBGroup_Change() 
MsgBox "TextBox " & CBGroup.Name & " changed" 
End Sub
And this in the userform module.
Code:
' UserForm module 

Dim CBs() As New CtrlClass 

Private Sub UserForm_Initialize() 
Dim CBCount As Integer 
Dim Ctrl As Control 
CBCount = 0 
For Each Ctrl In RegRap.Frame1.Controls 
If TypeName(Ctrl) = "TextBox" Then 
If Left$(Ctrl.Name, 4) = "Text" Then 
CBCount = CBCount + 1 
ReDim Preserve CBs(1 To CBCount) 
Set CBs(CBCount).CBGroup = Ctrl 
End If 
End If 
Next Ctrl 
End Sub
 
Upvote 0

al_b_cnu

Well-known Member
Joined
Jul 18, 2003
Messages
4,526
Hi,

the code
Code:
Private Sub CBGroup_Change() 
MsgBox "TextBox " & CBGroup.Name & " changed" 
End Sub

MUST be in a class module named 'CtrlClass'.

To do this in VBE from the menu Insert > Class Module. This will create a class module names 'Class1'.

Then change the name in the properties window to CtrlClass, (if the Properties window is not visible,press [F4])

Then paste the above code into it.

Remove the Sub 'CBGroup_Change'code from the userform code window.

[Edit] Thanks Norie,you beat me to it again,and so much more eloquently too :¬/

[Edit 2] ...and correct, unlike my submission too I missed the 'Public Witevents' statement :¬(
 
Upvote 0

Forum statistics

Threads
1,191,119
Messages
5,984,758
Members
439,909
Latest member
daigoku

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top