+ Reply to Thread
Results 1 to 19 of 19

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

  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:

    Please Login or Register  to view this content.
    And I got the correct result, but the format is not in percentage.

    The second code is:

    Please Login or Register  to view this content.
    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

  2. #2
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

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

    Take the quotes off of the "1" it makes it a string

    Please Login or Register  to view this content.
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

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

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

    Quote Originally Posted by xladept View Post
    Take the quotes off of the "1" it makes it a string

    Please Login or Register  to view this content.
    Tried but it did not work

  4. #4
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

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

    Try:

    Please Login or Register  to view this content.

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

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

    Quote Originally Posted by xladept View Post
    Try:

    Please Login or Register  to view this content.
    Still cannot get it to work, but I found a pattern. My If/Then command only works if it formats are either "0", "0.0", or "0.00". Not sure if it helps though.

  6. #6
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

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

    It turned red for me???

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

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

    Quote Originally Posted by xladept View Post
    It turned red for me???
    You mean both commands turned red for you?

  8. #8
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,528

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

    Is this of any help to you? (jindon's solution in Post #2)
    http://www.ozgrid.com/forum/showthread.php?t=167624

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

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

    Quote Originally Posted by jolivanes View Post
    Is this of any help to you? (jindon's solution in Post #2)
    http://www.ozgrid.com/forum/showthread.php?t=167624
    I changed from

    If Val(Replace(Me.TextBox2.Value, "%", "")) < 0

    to

    If Val(Replace(Me.TextBox2.Value, "%", "")) < 100

    and it worked! But I dont understand what does the command do?

  10. #10
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,014

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

    shamsul:

    Here's what I get without changing any of your code:

    Test.jpg

  11. #11
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

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

    Glad it's working

  12. #12
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,528

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

    Here is another one to play around with.
    Based on Post #8 info from jindon
    Attached Files Attached Files

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

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

    Try
    Please Login or Register  to view this content.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  14. #14
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,528

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

    But would it not be easier to format the textbox on the cell value?

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

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

    I'm not sure what you mean by "format the text box on the cell value"
    TextBox's don't have formats. They only contain strings, never numbers.

    A "numeral" is a string that represents a number. Textboxes can contain numerals, like "1" or "1.00" or "001". But Textboxes will never "see" them as equal, even though they represent the same number.

    When one wants to deal with the number that a TextBox.Text represents, one has to convert that string into a number. (The Val function in my code.)
    The relationship "less than" is a particular problem, because the string "10" is less than the string "2" (just as "Al" comes before "I" in the dictionary) while the numbers that they represent have the opposite relationship.

    It is easier to convert the numerals in text boxes to numbers than to fiddle with string based workarounds (e.g. making all numerals have the same number of characters like "10" vs "02").

    Convert the TextBox entries to numbers, handle those numbers as numbers, and then convert back to a string when putting the result back into a text box is the easiest approach for me.

    Excel VBA can be confusing around this issue because MicroSoft has programmed automatic type conversion into some of its features. But not all. It is better to explicitly convert between data types that to try to figure out (and remember and hope that the person who later uses your code also knows) if and when Excel will do data type conversions automatically.
    Last edited by mikerickson; 10-23-2016 at 03:39 PM.

  16. #16
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,528

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

    Sorry about the poor wording Mike.
    I meant the background color.
    We are putting a cell value into the textbox so if that value in the cell >50 then color the textbox orange.

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

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

    My post was responding to the OP that only mentioned Red and Green. (It looks like orange came from the linked question)

    From the posts in this thread, i'm guessing that someone is putting a % in a text box. I have difficulty convincing folks that if a text box always shows a percent, the coding is easier when there is a fixed Label with the % sign next to a text box with a pure numeral in it (only characters are 0123456789.-)

  18. #18
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,528

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

    I am not doubting your knowledge/wisdom by any means Mike.
    After reading some posts where people had trouble showing what they wanted to see in the textbox I thought along the lines of referencing the cell value.
    Just another way of tackling the problem.
    The orange is just any kind of color.

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

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

    Thats a good point of view.

    I think of a user form as a mostly stand-alone object that does thing to a workbook. But the notion that it is a co-user interface in conduction with the workbook is a useful way to approach it.

+ 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. [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