Change Back ground Colour of Text box on evnt( Enter, Exit, Value Changed)

ootkhopdi

Board Regular
Joined
Sep 30, 2013
Messages
68
Hi.
i have created a user form having 10 textboxes name tb1. tb2.....
i want to change background colour of text box,
when i enter , it becomes yellow,
when i change value of it, it changes to red
when i exit with no change it become normal colour..

how can i do it.
i have use code for enter.
Private Sub tb_EMPID_Enter()
Me.tb_EMPID.BackColor = vbRed
End Sub


but it change colour permanent after enter text box..

please give me solution or a way to solve this problem..
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
How about
Code:
Option Explicit
Dim tb1val As String

Private Sub tb1_Change()
   tb1.BackColor = vbRed
End Sub

Private Sub tb1_Enter()
   tb1.BackColor = vbYellow
   tb1val = tb1.Value
End Sub

Private Sub tb1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
   If tb1.Value = tb1val Then tb1.BackColor = vbWhite
End Sub
Where the textbox is called tb1
 
Upvote 0
Thansk Fluff. i got this..

but there is a problem. i want to this code run for every textbox of form, My textbox are 40 in number with diffrent type of format, Like Text, Date and Number...
so please give me help to this ..
as code run for every textbox. not for particular text box..
thanks once again..
 
Upvote 0
You'll need to create the relevant subs for each textbox, in the same manor as tb1.
ie with tb2 it would be
Code:
Option Explicit
Dim tb1val As String
Dim tb2val As String

Private Sub tb1_Change()
   tb1.BackColor = vbRed
End Sub

Private Sub tb1_Enter()
   tb1.BackColor = vbYellow
   tb1val = tb1.Value
End Sub

Private Sub tb1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
   If tb1.Value = tb1val Then tb1.BackColor = vbWhite
End Sub

Private Sub tb2_Change()
   tb2.BackColor = vbRed
End Sub

Private Sub tb2_Enter()
   tb2.BackColor = vbYellow
   tb2val = tb2.Value
End Sub

Private Sub tb2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
   If tb2.Value = tb2val Then tb2.BackColor = vbWhite
End Sub
 
Upvote 0
thanks fluff for early reply..but it will be a lengthy code and may be some error.

so i want to code for every text box..
thanks. i am also trying to write this..
 
Upvote 0

Forum statistics

Threads
1,213,527
Messages
6,114,150
Members
448,552
Latest member
WORKINGWITHNOLEADER

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