# Write one procedure to control all the fields on a form



## dellehurley

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


----------



## My Aswer Is This

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


----------



## My Aswer Is This

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


----------



## My Aswer Is This

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


----------



## dellehurley

My Aswer Is This said:


> 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.


----------



## My Aswer Is This

dellehurley said:


> This is what I was looking for Thank you very much.


Glad I was able to help you.
Come back here to Mr. Excel next time you need additional assistance.


----------

