+ Reply to Thread
Results 1 to 16 of 16

If Userform ListBox contains specific item then change label background colour.

  1. #1
    Registered User
    Join Date
    08-30-2012
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    37

    If Userform ListBox contains specific item then change label background colour.

    ...and if not, keep or revert back to original colour. Does this make sense to anybody? Your help would be very much appreciated.

    What I am trying to do is to find a specific value, in this case "Standard/National" upon entering the last of 3 listboxes (ListBox1) and changing the colour of the label's background (no caption) from grey to bright green in it exists, but back to grey if not (i.e. if the next search term is null).

    Thanks very much in advance for any help. :o)

  2. #2
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: If Userform ListBox contains specific item then change label background colour.

    maybe
    Please Login or Register  to view this content.
    change the names and colors as desired
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  3. #3
    Registered User
    Join Date
    08-30-2012
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    37

    Re: If Userform ListBox contains specific item then change label background colour.

    Hi JosephP,

    Thanks so much for your speedy assistance! However I have installed the code and I'm getting a 'Run-time error '13': Type Mismatch' error dialogue box. I didn't need to change any names in your code as they are the same names as my ListBox and Label anyway. Any ideas what could be doing this? Also does this need to be in the 'ListBox1_Change' sub or in the Userform_Initialize sub?

    Thanks again!
    Jamie

  4. #4
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: If Userform ListBox contains specific item then change label background colour.

    it depends when you want it to run but the listbox must be populated when it runs
    if it still errors can you provide a sample workbook?

  5. #5
    Registered User
    Join Date
    08-30-2012
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    37

    Re: If Userform ListBox contains specific item then change label background colour.

    Hi JP,

    Here it is, sorry for the delay - I had to make a new one (using exactly the same code) as the file was too large to upload. Your help and assistance is greatly appreciated!

    Thanks
    Jamie

    EDIT: Also yes, the listboxes are all populated/populating. Thanks.
    Attached Files Attached Files

  6. #6
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: If Userform ListBox contains specific item then change label background colour.

    there were no items in the list at that point. change the code to
    Please Login or Register  to view this content.
    to handle that and other eventualities :-)

  7. #7
    Registered User
    Join Date
    08-30-2012
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    37

    Re: If Userform ListBox contains specific item then change label background colour.

    Thanks JP, thank you SO much for this, you have been invaluable and you have saved me a lot of time! Just one thing though if you don't mind, the code works great but there is one thing it is not doing and that is reverting back to grey (or another colour of my choosing) if Standard/National is NOT in the list. Is this something easliy fixed or would I have to find different code for this?

    Thanks again!
    Jamie

  8. #8
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: If Userform ListBox contains specific item then change label background colour.

    just need an else clause
    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    08-30-2012
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    37

    Re: If Userform ListBox contains specific item then change label background colour.

    Absolutely 100% Perfect, works beautifully! Thanks for all your help, have a great day!

  10. #10
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: If Userform ListBox contains specific item then change label background colour.

    thank you-you too :-)

  11. #11
    Registered User
    Join Date
    08-30-2012
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    37

    Re: If Userform ListBox contains specific item then change label background colour.

    Hi JP, me again! Sorry to bother you, don't worry the code works fine and I'm just wondering whether it needs a slight adjustment or whether I have a glitch in my Excel program... (probably the latter knowing my luck), but could you tell me if this sounds like a code problem please?

    Here goes: When I'm running the UserForm all seems fine and it does what I requested (Picks out Standard/National from the list), however - what happens is that if my last Listbox contains more than one null enrty (i.e. 2 or more items that are NOT Standard/National), then the colours don't change... does this make sense to you? If so, do you think it's a code glitch or an Excel glitch?

    Thanks in advance, and sorry to bother you again!

  12. #12
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: If Userform ListBox contains specific item then change label background colour.

    could be either ;-)
    can you post a sample that demonstrates the problem?

  13. #13
    Registered User
    Join Date
    08-30-2012
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    37

    Re: If Userform ListBox contains specific item then change label background colour.

    Hi JP, thanks! Okay, the problem is like this:

    If 'Standard/National' in list then label colour changes to green (Correct)
    If 'Standard National' in list with other entries then label changes to green (Correct)
    If 'Standard National' NOT in list but 1 null entry is, label changes to red (Correct)
    If 'Standard National' NOT in list but MORE THAN 1 null entry is, label does NOT change colour. (Incorrect)

    So it's just the last of the above that is causing slight problems, I was just wondering if you knew of any code to fix this? Thanks again!

  14. #14
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: If Userform ListBox contains specific item then change label background colour.

    two changes required
    Please Login or Register  to view this content.
    and change this line in listboxp_change
    Please Login or Register  to view this content.
    to
    Please Login or Register  to view this content.

  15. #15
    Registered User
    Join Date
    08-30-2012
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    37

    Re: If Userform ListBox contains specific item then change label background colour.

    As always JP, you are a legend! That's it sorted completely now. I've thoroughly tested all the bad spots and they are working fine now, many thanks for your kind assistance and patience! I would like to buy you a beer, where can I send it to?

  16. #16
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: If Userform ListBox contains specific item then change label background colour.

    next time I'm in London I'll let you know ;-)

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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