userform text fields with the same code....

RobbieC

Active Member
Joined
Dec 14, 2016
Messages
345
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:

Logit

Well-known Member
Joined
Aug 31, 2016
Messages
2,920
.
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
 

Forum statistics

Threads
1,081,691
Messages
5,360,631
Members
400,592
Latest member
badgergurl

Some videos you may like

This Week's Hot Topics

  • VBA (Userform)
    Hi All, I just would like to know why my code isn't working. Here is my VBA code: [CODE=vba]Private Sub OKButton_Click() Dim i As Integer...
  • List box that changes fill color
    Hello, I have gone through so many pages trying to figure this out. I have a 2020 calendar that depending on the day needs to have a certain...
  • Remove duplicates and retain one. Cross-linked cases
    Hi all I ran out of google keywords to use and still couldn't find a reference how to achieve the results of a single count. It would be great if...
  • VBA Copy and Paste With Duplicates
    Hello All, I'm in need of some input. My VBA skills are sub-par at best. I've assembled this code from basic research and it works but is...
  • Macro
    is it possible for a macro to run if the active cell value is different to the value above it
  • IF DATE and TIME
    I currently use this to check if date has passed but i also need to set a time on it too. Is it possible? [CODE=vba]=IF(B:B>TODAY(),"Not...
Top