PDA

View Full Version : [SOLVED] Formatting multiple textboxes on a UserForm



rrtts
01-17-2008, 01:15 PM
I have a series of textboxes on a userform. I'd like to color code the textboxes based on their value in order to easily distinguish similar values.

I have used a code similar to this in the past, but now I have roughy 40 textboxes with 10 different values that can be selected. To use the code below for each value on every textbox would be rather lengthy and very inefficient. However, I can't seem to figure out an alternative solution.

Any help would be greatly appreciated.



If Userform1.Textbox1.value = "xxx" Then Userform1.Textbox1.BackColor = RGB(xxx, xxx, xxx)

rconverse
01-17-2008, 02:03 PM
I have a series of textboxes on a userform. I'd like to color code the textboxes based on their value in order to easily distinguish similar values.

I have used a code similar to this in the past, but now I have roughy 40 textboxes with 10 different values that can be selected. To use the code below for each value on every textbox would be rather lengthy and very inefficient. However, I can't seem to figure out an alternative solution.

Any help would be greatly appreciated.


If Userform1.Textbox1.value = "xxx" Then Userform1.Textbox1.BackColor = RGB(xxx, xxx, xxx)


You could use a loop or "For Each" and then set if or case statements for the values. That should reduce the code quit a bit.

HTH,
Roger

Bob Phillips
01-17-2008, 02:48 PM
Private Sub TextBox1_Change()
Call TB_Colour(Me.TextBox1)
End Sub

Private Sub TextBox2_Change()
Call TB_Colour(Me.TextBox2)
End Sub

'etc.

Private Sub TB_Colour(ByRef tb As MSForms.TextBox)
Select Case tb.Value
Case "xxx": tb.BackColor = RGB(xxx, xxx, xxx)
Case "yyy": tb.BackColor = RGB(xxx, yyy, xxx)
Case "zzz": tb.BackColor = RGB(xxx, xxx, zzz)
'etc.
End Select
End Sub

mikerickson
01-17-2008, 06:16 PM
Nice.

rrtts
01-18-2008, 07:35 AM
That is genius. Works briliiantly.