Subroutine inside class not firing

Shameem Khan

New Member
Joined
Aug 9, 2020
Messages
13
Office Version
  1. 2013
Platform
  1. Windows
Hello,

I have 30 textboxes in my Userform (Userform1) which are named TB11,TB12,TB13..... etc and I am using a textbox_change Sub inside a class to change the setfocus to the next textbox after any character is entered in one textbox. But for some reason, my sub inside the class is not firing when I enter a character in the textbox. Can anyone help me find what I am doing wrong ?

Thanks in Advance !

Inside the Userform1:
VBA Code:
Private Sub UserForm_Initialize()

    TB11.SetFocus
   
    Dim c As Control
    Dim tb As New Class1

    For Each c In Me.Controls
        If TypeName(c) = "TextBox" Then
            Set tb.txtbox = c
        End If
    Next c
End Sub

Inside Class1:
VBA Code:
Public WithEvents txtbox As MSForms.TextBox

Public Sub txtbox_Change()
    Dim a As Integer
    If Len(txtbox) = 1 Then
        a = Right(txtbox.Name, 2) + 1
        UserForm1.Controls("TB" & a).SetFocus
    End If
End Sub
 
Last edited by a moderator:

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Your class instance is declared within Userform_Initialize so as soon as that code is finished, your variable is destroyed. You're also assigning each textbox to the same class instance so you would only ever sink events for the last textbox assigned. You need a separate instance for each textbox, and a module level variable to hold all the class instances. For example:

VBA Code:
Dim ClassCollection as Collection
Private Sub UserForm_Initialize()
Set ClassCollection = New Collection ' this will hold the instances of your class
TB11.SetFocus

Dim c As Control
Dim tb As Class1

For Each c In Me.Controls
If TypeName(c) = "TextBox" Then
set tb = New Class1
Set tb.txtbox = c
ClassCollection.Add tb
End If
Next c
End Sub
 
Upvote 0
Solution
Yup. That worked great. Thanks.
Just out of curiosity, will an array work instead of collection ?
 
Upvote 0
Yes, you could use an array, or a dictionary, or anything that can contain multiple objects.
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,255
Members
449,075
Latest member
staticfluids

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
Back
Top