Run a macro if any of textboxes change - vba

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
I have about 50 textboxes that I want to run a certain macro each time their contents change.
I was doing it individually - is there a way I could loop through the textboxes with a function or anything similar?

The textboxes are on a userform
Thanks in advance
 
Putt a break point at the Call MyMacro line, then start the code that loads the form. Does execution stop at the break point?
 
Last edited:
Upvote 0

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Click on the far left margin of the code window. The line of code will be highlighted in a dark maroon or brown color and there will be a circle of the same color in the margin. The code will pause when it gets to the breakpoint. You can then step through using the F8 key to see where the code goes.
 
Upvote 0
Yes
You did change "MyMacro" to the real name of your macro ?

Yes I did change it.

It is working now for the initialize event but not for when I change content of the textboxes
 
Upvote 0
Here is a portion of the code you are seeking. Review the entire code in the download workbook (ThisWorkbook module - UserForm code - Class Module).

VBA Code:
Public ctl_collection As New Collection
Private Sub Userform_Initialize()
    For Each ct In Controls
        If TypeName(ct) = "TextBox" Then
            ctl_collection.Add New inputcheck, ct.Name
            Set ctl_collection(ct.Name).cl_textbox = ct
        End If
    Next
End Sub

Download example workbook : Test.xlsm
 
Upvote 0
Here is a portion of the code you are seeking. Review the entire code in the download workbook (ThisWorkbook module - UserForm code - Class Module).

VBA Code:
Public ctl_collection As New Collection
Private Sub Userform_Initialize()
    For Each ct In Controls
        If TypeName(ct) = "TextBox" Then
            ctl_collection.Add New inputcheck, ct.Name
            Set ctl_collection(ct.Name).cl_textbox = ct
        End If
    Next
End Sub

Download example workbook : Test.xlsm

Wow - I can't believe my eyes!

Thanks for the code.

My textboxes are number like this txt1 txt2 and so on.

Is there a way to let the code respond to just txt4 to txt50?
 
Upvote 0
The code will not run when a text box changes, because it's not in a TextBox_Change event procedure.

Instead of the UserForm_Initialize procedure, put the call to your macro in the UserForm_MouseMove procedure. Now it will be called whenever the mouse moves over the form.
 
Upvote 0
"Is there a way to let the code respond to just txt4 to txt50?"

One way:

Code:
Dim iTxt as long
iTxt = Val(Mid$(ctrl.name, 4)
If iTxt >= 4 And iTxt <= 50 Then
  ' do your stuff
End If

Another way: Put a descriptive string into the Tag property of the textboxes you want to test. Then

Code:
If ctrl.Tag = "My String" Then
  '' do your stuff
End If
 
Upvote 0
Here is a portion of the code you are seeking. Review the entire code in the download workbook (ThisWorkbook module - UserForm code - Class Module).

VBA Code:
Public ctl_collection As New Collection
Private Sub Userform_Initialize()
    For Each ct In Controls
        If TypeName(ct) = "TextBox" Then
            ctl_collection.Add New inputcheck, ct.Name
            Set ctl_collection(ct.Name).cl_textbox = ct
        End If
    Next
End Sub

Download example workbook : Test.xlsm

Good evening
 
Upvote 0

Forum statistics

Threads
1,214,832
Messages
6,121,843
Members
449,051
Latest member
excelquestion515

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