Results 1 to 19 of 19

VBA Changes TextBox.BackColor, but keeps getting different results.

Threaded View

  1. #1
    Registered User
    Join Date
    08-13-2013
    Location
    Jakarta
    MS-Off Ver
    Excel 2013
    Posts
    38

    Post VBA Changes TextBox.BackColor, but keeps getting different results.

    I tried to make a textbox in a userform changes its background, green if less than 100% and red if more or equal to 100%. So far I made 2 different codes and I got 2 different results!

    The first code is:

    Private Sub CommandButton1_Click()
    
    TextBox1.Value = Sheets("Sheet1").Range("I9").Value
    TextBox1.Value = Format(TextBox1.Value, "0.00")
    
    If TextBox1.Value >= "1" Then
    TextBox1.BackColor = vbRed
    Else: TextBox1.BackColor = vbGreen
    End If
    
    End Sub
    And I got the correct result, but the format is not in percentage.

    The second code is:

    Private Sub CommandButton2_Click()
    
    TextBox2.Value = Sheets("Sheet1").Range("I9").Value
    TextBox2.Value = Format(TextBox2.Value, "0.00%")
    
    If TextBox2.Value >= "1" Then
    TextBox2.BackColor = vbRed
    Else: TextBox2.BackColor = vbGreen
    End If
    
    End Sub
    I managed to change the format to percentage, but I got the background color wrong.

    What is wrong with my code in the second one? I did the same thing, except that I changed the format from 0.00 to 0.00%, and somehow I got the background the other way around.

    Is there any way for me to rectify this? I want to display it as percentage, and get the background color to be green if its less than 100%, and red if equal or more than 100%.

    Thank you in advance.
    Attached Files Attached Files

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Have search results by using textbox
    By YasserKhalil in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-21-2015, 05:33 AM
  2. [SOLVED] Change the properties of a userforn textbox based on the backcolor of a label
    By chipnputt in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-15-2014, 10:47 PM
  3. ActiveX Textbox BackColor
    By heather1209 in forum Excel General
    Replies: 6
    Last Post: 06-04-2014, 11:53 AM
  4. Change TextBox BackColor
    By bdb1974 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-29-2010, 05:07 PM
  5. Change Textbox Backcolor (multiple textboxes)
    By msommerf in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-05-2010, 10:52 AM
  6. Change textbox backcolor then save workbook?
    By danny2000 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-17-2008, 07:52 AM
  7. TextBox BackColor
    By Lenny_821 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-15-2005, 01:13 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