Apply code to predetermined textboxes

sarasotavince

New Member
Joined
May 14, 2011
Messages
24
After some unsuccessful searching, I am wondering if there is a way to apply the same kind of coding to multiple textboxes. I have seen where one call apply code to ALL textboxes on a userform, but I want to apply the code to only specific textboxes.

On the userform I have 30 textboxes. Some I have restricted to numbers only, some letters only, others are a combination. All the letter only boxes are converted to UPPERCASE. Here is the code I use:

Code:
Private Sub TextBox8_Change()
'MAKE UPPER CASE
On Error Resume Next
TextBox8 = UCase(TextBox8)
On Error GoTo 0
End Sub

Now the question. Is there a way Without copying this 20 times and changing the textbox#, how would I apply this to textboxes 1,3,5,6,10,12, etc. only?

I'm ready to take my (code)monkeying to the next level. Thanks!
 
To respond to Mikerickson:

Did you rename the class module CommonTextBox?
Yes, I did. And I've double checked to be sure I copied everything correctly. Still no joy.

There is also an error exiting the userform I did not mention because I couldn't get the first part to work (yet).
Code:
For Each aCommonTbx In myTextBoxes
The error code is 424, Object required.

I'm still a ways from understanding the code but I'll keep trying.
 
Upvote 0

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
To respond to Jaafer:

Your code works well, if I wanted to use UPPERCASE in every textbox. I was trying to limit the code to other those textboxes I had defined (Textbox1, Textbox4, Textbox12, etc.)

But it is good stuff and I'm sure I can use it in the future.
 
Upvote 0
....There is also an error exiting the userform I did not mention because I couldn't get the first part to work (yet).
Code:
For Each aCommonTbx In myTextBoxes
The error code is 424, Object required. .
That's a big clue. I missed the declaration of the common variable in the userform's code module.
This is what should do in the uf code module

Code:
Option Explicit

[COLOR="Red"]Dim MyTextBoxes As New Collection[/COLOR]

Private Sub UserForm_Initialize()
    Dim oneTextBox As Variant
    Dim aCommonTbx As CommonTextBox
    Set MyTextBoxes = New Collection
    
    For Each oneTextBox In Array(TextBox1, TextBox3, TextBox4)

        Rem create new instance of CommonTextBox
        Set aCommonTbx = New CommonTextBox

        Rem assign userform text box to new instance
        Set aCommonTbx.aTxtBox = oneTextBox

        Rem save new instance in collection
        MyTextBoxes.Add aCommonTbx, Key:=oneTextBox.Name
        
        Rem housekeeping
        Set aCommonTbx = Nothing
    Next oneTextBox
End Sub

Private Sub UserForm_Terminate()
    Rem good practice of cleaning up user defined objects
    Dim aCommonTbx As CommonTextBox

    Rem de-instansize all the CommonTextBox objects
    For Each aCommonTbx In MyTextBoxes
        Set aCommonTbx = Nothing
    Next aCommonTbx

    Rem housekeeping
    Set MyTextBoxes = Nothing
End Sub
 
Upvote 0
That did the trick. It's a lot to study but now it's a working model.

I did add the following code to the class module to limit input to letters only. In the textboxes that need other coding, I'll do those manually.


Code:
[SIZE=3][FONT=Calibri]Select Case KeyAscii<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]Case 0 To 64, 91 To 96, 123 To 127<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]KeyAscii = vbNull<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]End Select<o:p></o:p>[/FONT][/SIZE]

Thanks again mikerickson
 
Upvote 0

Forum statistics

Threads
1,223,234
Messages
6,170,891
Members
452,366
Latest member
TePunaBloke

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