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
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Sometimes when I have to do seemingly repetitive things like this, I use individual textbox_change event, but from each I call a general function and pass in a reference to the textbox which changed.
 
Upvote 0
Sometimes when I have to do seemingly repetitive things like this, I use individual textbox_change event, but from each I call a general function and pass in a reference to the textbox which changed.

That's what I am doing at the moment - I was thinking there could be another way around it.

Thanks for the concern.
 
Upvote 0
.
Presuming your textboxes are located on a USER FORM :

VBA Code:
Private Sub UserForm_Initialize()

Dim ctrl As Control      ' CREATE A CONTROL OBJECT.

    ' LOOP THROUGH EACH CONTROL, CHECK IF THE CONTROL IS A TEXTBOX.
    For Each ctrl In Me.Controls
        If TypeName(ctrl) = "TextBox" Then
            Call MyMacro
        End If
    Next ctrl
    
End Sub
 
Upvote 0
You can set up a class module for a textbox, and put the event code in the class. Then when you initialize the form, you create an instance of the class for each textbox in the form. When text in a textbox changes, it will run code in the class rather than in the userform. This is long and tedious, and I usually only do it if there are lots of controls I need the same code for, or I need the same code for controls in different forms.
 
Upvote 0
Another approach would be to place all the textboxes in a GROUP BOX control. You can set the properties for all textboxes that are
contained within the GROUP BOX control at one time.
 
Upvote 0
Group Box? You mean a Frame? You can simply ctrl-select multiple controls and change their properties together. But that doesn't help with the coding situation here.
 
Upvote 0
.
Presuming your textboxes are located on a USER FORM :

VBA Code:
Private Sub UserForm_Initialize()

Dim ctrl As Control      ' CREATE A CONTROL OBJECT.

    ' LOOP THROUGH EACH CONTROL, CHECK IF THE CONTROL IS A TEXTBOX.
    For Each ctrl In Me.Controls
        If TypeName(ctrl) = "TextBox" Then
            Call MyMacro
        End If
    Next ctrl
    
End Sub

The code didn't work for me.
 
Upvote 0
What does "didn't work" mean? Did the initialize event not fire? Did it not recognized the textboxes? Did the called macro not run?

Thus should probably not be in the initialize event procedure though. It should run after the firm is displayed, where the user may have changed something.
 
Upvote 0
What does "didn't work" mean? Did the initialize event not fire? Did it not recognized the textboxes? Did the called macro not run?

Thus should probably not be in the initialize event procedure though. It should run after the firm is displayed, where the user may have changed something.

The called macro didn’t run.
 
Upvote 0

Forum statistics

Threads
1,214,950
Messages
6,122,436
Members
449,083
Latest member
Ava19

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