userform text fields with the same code....

RobbieC

Active Member
Joined
Dec 14, 2016
Messages
376
Office Version
  1. 2010
Platform
  1. Windows
Hi there, I have a userform with 100 text fields on it. they are all named dp1 dp2 dp3.... dp99 dp100

when I enter a text field I want the backcolor to change to green and the text white. and upon exiting revert to backcolor white and black text... I can do this using this code:

Code:
Private Sub dp1_enter()
Me.dp1.BackColor = &H8000&
Me.dp1.ForeColor = &HFFFFFF
End Sub


Private Sub dp1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Me.dp1.BackColor = &HFFFFFF
Me.dp1.ForeColor = &H80000008
End Sub

but is there a way to do a 'catch all' bit of code, or do I have to manually enter this code for all 100 text fields?

If you can point me in the right direction, I'd be very happy.

Thanks

Rob
 
Last edited:

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
.
Loop Through Specific/Specified Controls

[FONT=&quot]Use the code below to loop through only specified Controls on a UserForm.

[/FONT]

Code:
[COLOR=#000000][FONT=&quot]
[/FONT][/COLOR]
[B]Private Sub CommandButton1_Click()[/B]

Dim cCont As Control



    For Each cCont In Me.Controls

        If TypeName(cCont) = "TextBox" Then

            'DO STUFF HERE

        End If

     Next cCont



[B]End Sub[/B]

https://www.ozgrid.com/VBA/control-loop.htm
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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