Write one procedure to control all the fields on a form

dellehurley

Board Regular
Joined
Sep 26, 2009
Messages
171
Office Version
  1. 365
Platform
  1. Windows
Hi
Is it possible to write one procedure in VBA so that upon Entry to textbox, combobox etc something happens eg the back colour changes?
I know how to do it is they are all named similarly eg. textbox1, textbox2, textbox3 etc but can I do it if the names are all very different eg, textbox1, cmbName, txtAddress etc

Thanks
Dannielle
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Are all of these controls the same type?
Like all are Textboxes or do you mean all controls on the UserForm

So tell me exactly what you want to do to all of these controls so I can write a script
 
Upvote 0
Here is a example:
VBA Code:
Private Sub CommandButton1_Click()
'Modified  10/15/2022  9:25:01 PM  EDT
For Each Control In Me.Controls
    Control.BackColor = vbRed
Next
End Sub
 
Upvote 0
Or this if we are just concerned about all Textboxes on Userform
VBA Code:
Private Sub CommandButton1_Click()
'Modified  10/15/2022  9:35:01 PM  EDT
For Each Control In Me.Controls
    If TypeOf Control Is MSForms.TextBox Then Control.BackColor = vbRed
Next
End Sub
 
Upvote 0
Solution
Or this if we are just concerned about all Textboxes on Userform
VBA Code:
Private Sub CommandButton1_Click()
'Modified  10/15/2022  9:35:01 PM  EDT
For Each Control In Me.Controls
    If TypeOf Control Is MSForms.TextBox Then Control.BackColor = vbRed
Next
End Sub
This is what I was looking for Thank you very much. (y)
 
Upvote 0

Forum statistics

Threads
1,215,025
Messages
6,122,731
Members
449,093
Latest member
Mnur

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