+ Reply to Thread
Results 1 to 22 of 22

Userform textbox background color and percent

Hybrid View

  1. #1
    Registered User
    Join Date
    05-28-2013
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    73

    Userform textbox background color and percent

    I am trying to get the text box to change background color when the percent is < 90.0% and > 110.0% and to change back when the value is in between. However it only does red in the current version below. When I change the "90.0" to "0.9" and so forth it will only work for number entered 0.9-1.1. I have tried many things but know I must be missing something.


    Private Sub Grade_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Grade.value = Format(Grade.value / 100, "0.0%")
    
     Grade.value = Grade
       
     If Grade < "90.0" Then
           Grade.BackColor = &HFF&
           
        ElseIf Grade > "110.0" Then
            Grade.BackColor = &HFF&
         
        ElseIf Grade >= "90.0" And Grade <= "110.0" Then
             Grade.BackColor = CoreNumber.BackColor
     
        End If
    End Sub

  2. #2
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Userform textbox background color and percent

    Try this
    Private Sub Grade_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    
    Dim GrdNo as long
    GrdNo = val(Grade.Value)
    
    Grade.value = Format(Grade.value / 100, "0.0%")
    
       
     If GrdNo < 90 Then
           Grade.BackColor = &HFF&
           
        ElseIf GrdNo > 110 Then
            Grade.BackColor = &HFF&
         
        ElseIf GrdNo >= 90 And GrdNo <= 110 Then
             Grade.BackColor = CoreNumber.BackColor
     
        End If
    End Sub
    Last edited by Kyle123; 01-06-2015 at 09:04 AM.

  3. #3
    Registered User
    Join Date
    05-28-2013
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    73

    Re: Userform textbox background color and percent

    It works with the correct range except it doesn't keep the format correctly it changes 100.0 to just 100 without it being 100.0%

  4. #4
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Userform textbox background color and percent

    oops, typo, try that

  5. #5
    Registered User
    Join Date
    05-28-2013
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    73

    Re: Userform textbox background color and percent

    That works except for numbers that round to 90 such as 89.9 or 110. That should be turned red but is not. It needs to be 90.0 and 110.0 but doesn't work.

  6. #6
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,169

    Re: Userform textbox background color and percent

    You could use:
    GrdNo = Application.Round(val(Grade.Value), 1)
    Everyone who confuses correlation and causation ends up dead.

  7. #7
    Forum Contributor
    Join Date
    12-24-2014
    Location
    little ole England
    MS-Off Ver
    2013
    Posts
    116

    Re: Userform textbox background color and percent

    could try >= 89.5 instead of >=90



    EDIT

    also change < 90 to < 89.5
    ◄Ŧя?μвŁ?►
    By Name & By Nature

  8. #8
    Registered User
    Join Date
    05-28-2013
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    73

    Re: Userform textbox background color and percent

    This doesn't work either

    Private Sub Grade_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    
    Dim GrdNo as long
    GrdNo = Application.Round(val(Grade.Value), 1)
    
    Grade.value = Format(Grade.value / 100, "0.0%")
    
       
     If GrdNo <= 89.9 Then
           Grade.BackColor = &HFF&
           
        ElseIf GrdNo >= 110.1 Then
            Grade.BackColor = &HFF&
         
        ElseIf GrdNo >= 90 And GrdNo <= 110 Then
             Grade.BackColor = CoreNumber.BackColor
     
        End If
    End Sub

  9. #9
    Registered User
    Join Date
    05-28-2013
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    73

    Re: Userform textbox background color and percent

    Didn't work either. The number 89.9 should turn red where as 90.0 should not change.

  10. #10
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Userform textbox background color and percent

    Being thick

    Dim GrdNo as Double

  11. #11
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,169

    Re: Userform textbox background color and percent

    Ha - I didn't even see that line!

  12. #12
    Registered User
    Join Date
    05-28-2013
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    73

    Re: Userform textbox background color and percent

    Changing long to double works, working code below

    Private Sub Grade_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    
    Dim GrdNo as Double
    GrdNo = Application.Round(val(Grade.Value), 1)
    
    Grade.value = Format(Grade.value / 100, "0.0%")
    
       
     If GrdNo <90 Then
           Grade.BackColor = &HFF&
           
        ElseIf GrdNo >110 Then
            Grade.BackColor = &HFF&
         
        ElseIf GrdNo >= 90 And GrdNo <= 110 Then
             Grade.BackColor = CoreNumber.BackColor
     
        End If
    End Sub

  13. #13
    Registered User
    Join Date
    05-28-2013
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    73

    Re: Userform textbox background color and percent

    If I don't change anything in the cell after being in it I get a run-time error 13 type mismatch and highlights. I have a my original code that works for non % when I exit out of the box without changing anything. it doesn't matter if it is at exit or after update. Any ideas?

    Grade.value = Format(Grade.value / 100, "0.0%")

  14. #14
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,169

    Re: Userform textbox background color and percent

    Try:
    Private Sub Grade_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    
        Dim GrdNo                 As Double
        GrdNo = Application.Round(CDbl(Replace(Grade.Value, "%", "")), 1)
    
        Grade.Value = Format(GrdNo / 100, "0.0%")
    
    
        If GrdNo < 90 Then
            Grade.BackColor = &HFF&
    
        ElseIf GrdNo > 110 Then
            Grade.BackColor = &HFF&
    
        ElseIf GrdNo >= 90 And GrdNo <= 110 Then
            Grade.BackColor = CoreNumber.BackColor
    
        End If
    End Sub

  15. #15
    Registered User
    Join Date
    05-28-2013
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    73

    Re: Userform textbox background color and percent

    Same result

  16. #16
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,169

    Re: Userform textbox background color and percent

    What do you want to happen if there was a value in there and you delete it? What should the backcolor be?

  17. #17
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Userform textbox background color and percent

    What is a typical entry in the text box?
    What is shown if this is inserted before the If statement?
    MsgBox GrdNo
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  18. #18
    Registered User
    Join Date
    05-28-2013
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    73

    Re: Userform textbox background color and percent

    Quote Originally Posted by mikerickson View Post
    What is a typical entry in the text box?
    What is shown if this is inserted before the If statement?
    MsgBox GrdNo
    I get 100 in the message box after typing in 100.0. After closing the box it displays 100.0% in the textbox like it should

  19. #19
    Registered User
    Join Date
    05-28-2013
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    73

    Re: Userform textbox background color and percent

    The color should be

    Grade.BackColor = CoreNumber.BackColor
    If I enter a number and go to the next box and then return to the this box, but then move onto another box without changing anything or deleting and exiting the box I get the error.

    It basically doesn't like it when there are no changes made while being in the box and exiting the box

  20. #20
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,169

    Re: Userform textbox background color and percent

    Try this - should cover most things:
    Private Sub Grade_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    
        Dim GrdNo                 As Double
        Dim strText               As String
    
        If Len(Grade.Value) <> 0 Then
        
            strText = Replace$(Grade.Value, "%", "")
            
            If IsNumeric(strText) Then
            
                GrdNo = Application.Round(CDbl(strText), 1)
                Grade.Value = Format(GrdNo / 100, "0.0%")
    
                If GrdNo < 90 Then
                    Grade.BackColor = &HFF&
    
                ElseIf GrdNo > 110 Then
                    Grade.BackColor = &HFF&
    
                ElseIf GrdNo >= 90 And GrdNo <= 110 Then
                    Grade.BackColor = CoreNumber.BackColor
                End If
            Else
                MsgBox "No text please"
                Cancel = True
    
            End If
            
        Else
            Grade.BackColor = CoreNumber.BackColor
        End If
    End Sub

  21. #21
    Registered User
    Join Date
    05-28-2013
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    73

    Re: Userform textbox background color and percent

    Quote Originally Posted by romperstomper View Post
    Try this - should cover most things:
    Private Sub Grade_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    
        Dim GrdNo                 As Double
        Dim strText               As String
    
        If Len(Grade.Value) <> 0 Then
        
            strText = Replace$(Grade.Value, "%", "")
            
            If IsNumeric(strText) Then
            
                GrdNo = Application.Round(CDbl(strText), 1)
                Grade.Value = Format(GrdNo / 100, "0.0%")
    
                If GrdNo < 90 Then
                    Grade.BackColor = &HFF&
    
                ElseIf GrdNo > 110 Then
                    Grade.BackColor = &HFF&
    
                ElseIf GrdNo >= 90 And GrdNo <= 110 Then
                    Grade.BackColor = CoreNumber.BackColor
                End If
            Else
                MsgBox "No text please"
                Cancel = True
    
            End If
            
        Else
            Grade.BackColor = CoreNumber.BackColor
        End If
    End Sub
    This works, I added the line of code wrong the first time I tried it
    Last edited by jh51745; 01-06-2015 at 10:56 AM.

  22. #22
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,169

    Re: Userform textbox background color and percent

    Can you post a workbook. That works fine for me and handles blank cells, text and numbers or percentages.

+ 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. In Userform how can I change the background color of a Frame
    By mattress58 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-13-2014, 12:27 PM
  2. [SOLVED] UserForm TextBox formats - Display as Decimal, Read as Percent. Possible?
    By mc84excel in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-12-2013, 11:40 PM
  3. Userform Command Button Background Color
    By AaronB in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 03-19-2012, 01:02 PM
  4. Can background color of frame change on userform by combobox selection on userfor
    By teacher_rob in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 11-12-2011, 05:47 PM
  5. Textbox background color
    By Justme47 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-07-2009, 01:58 PM

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