+ Reply to Thread
Results 1 to 5 of 5

Changing colour of text box when data is added.

Hybrid View

  1. #1
    Registered User
    Join Date
    10-28-2010
    Location
    durham
    MS-Off Ver
    Excel 2003
    Posts
    4

    Changing colour of text box when data is added.

    I have a userform with around 100 text boxes, these are populated from a worksheet. however if there is no data on the sheets then the text box back colour is yellow to highlight it. If data is then added, I want the colour to change to white. I can do this individually using the textbox(number)_change backcolour but is there a way to code for all text boxes change instead of 100 different short codes

  2. #2
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Changing colour of text box when data is added.

    Try this...

    Add a new userform and 2 textboxes. Paste the following in the userform code module:
    Option Explicit
    Dim TBOXES() As New Class1    '// **** NOTE - Edited. Make sure you have this version ****
    Private Sub UserForm_Initialize()
    
        Dim Counter As Integer
        Dim Obj As Control
        
        For Each Obj In Me.Controls
            If TypeOf Obj Is MSForms.TextBox Then
                Counter = Counter + 1
                ReDim Preserve TBOXES(1 To Counter)
                Set TBOXES(Counter).AssignTB = Obj
            End If
            
        Next Obj
        
        Set Obj = Nothing
        
    End Sub
    Add a new CLASS module. Paste the following to the class:
    Option Explicit
    
    Public WithEvents TB As MSForms.TextBox
    
    Public Property Set AssignTB(t As MSForms.TextBox)
        
        Set TB = t
        
        '// force change of colour
        TB_Change
        
    End Property
    
    
    Private Sub TB_Change()
    
        TB.BackColor = IIf(TB.Text = vbNullString, &HC0FFFF, &H80000005)
    
    End Sub
    
    Private Sub TB_MouseDown(ByVal Button As Integer, _
        ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
        
        With TB
             ' Do something
        End With
        
    End Sub
    Leave all names as the default, the Class should be named Class1. Hopefully you do not have any other classes (Doubtful ) but it's easy enough to change.

    Try running that (the userform, not your complete project) and see if you can understand what's going on... The key to this is the declaration using WITHEVENTS in the Class and the array of classes in the userform.

    After running once, add another textbox or 2. No coding changes will be needed for the new textboxes to respond to changes. (Hopefully!)
    Last edited by cytop; 02-02-2014 at 08:26 AM. Reason: Clarification

  3. #3
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Changing colour of text box when data is added.

    There are two ways to do this.

    The simplest is to use a subroutine

    Untested Code:
    Sub textbox1_change()
    TextChange (1)
    End Sub
    Sub textbox2_change()
    TextChange (2)
    End Sub
    
    Sub TextChange(TbNo As Integer)
    
    If Len(Me.Controls("Textbox" & TbNo).Text) = "" Then
    Me.Controls("Textbox" & TbNo).BackColor = vbWhite
    Else
    Me.Controls("Textbox" & TbNo).BackColor = vbYellow
    End If
    End Sub
    You will need 100 of the textbox change sub routines.

    But these can be created in excel using another macro and then cut and pasted into visual basic.

    
    Sub Macro1()
    
        Range("A1").FormulaR1C1 = _
            "=""Sub Textbox"" & CEILING( ROW()/3,1)& ""_Change()"""
        Range("A2").FormulaR1C1 = "TextChange"
        Range("A3").FormulaR1C1 = "End Sub"
        Range("A1:A3").Copy Destination:=Range("A1:A300")
        Range("A1:A300").Value = Range("A1:A300").Value
    End Sub

    ___________________________________________________________________________________________________________________________________________________________

    The Second is to create a new cllass of object.

    This shows you how:

    http://www.ozgrid.com/forum/showthread.php?t=141695
    Last edited by mehmetcik; 02-02-2014 at 08:57 AM.

  4. #4
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,444

    Re: Changing colour of text box when data is added.

    Use a class to handle the change event for numerous textboxes.
    Attached Files Attached Files
    Cheers
    Andy
    www.andypope.info

  5. #5
    Registered User
    Join Date
    10-28-2010
    Location
    durham
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Changing colour of text box when data is added.

    Thanks for the prompt replies, I have gone with cytop's reply which is working a treat. I am trying to pick the code apart to understand what is happening. thanks again

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Changing colour on changing a Data Validation List added with VBA
    By e4excel in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-16-2011, 08:15 AM
  2. changing all text boxes of one fill colour to another colour
    By was0696 in forum Word Formatting & General
    Replies: 3
    Last Post: 02-18-2010, 05:57 AM
  3. Excel 2007 : Changing text colour within a strings
    By Pigmy in forum Excel General
    Replies: 4
    Last Post: 10-21-2009, 07:41 AM
  4. Changing the text colour on a button
    By Robin Quinnell in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-09-2007, 09:21 AM
  5. Replies: 3
    Last Post: 04-23-2005, 06:06 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1