+ Reply to Thread
Results 1 to 19 of 19

Dynamically change the color of all labels on a worksheet

  1. #1
    Registered User
    Join Date
    09-04-2014
    Location
    WA, USA
    MS-Off Ver
    2007
    Posts
    69

    Dynamically change the color of all labels on a worksheet

    Below is the code I am attempting to make work to dynamically change the color of all labels on my worksheet. I get the error message "Object doesn't support this property or method".

    I can use this code to change other properties of the label (ex. It's position). Is there a way to reference my labels dynamically in a different way? I do know you can substitute 'OLEObjects' for 'Shapes', (which seems to work better in some codes?), but it doesn't help in this case.



    Please Login or Register  to view this content.

  2. #2
    Registered User
    Join Date
    09-04-2014
    Location
    WA, USA
    MS-Off Ver
    2007
    Posts
    69

    Re: Dynamically change the color of all labels on a worksheet

    This is how I am currently solving the problem. But I worry that it is becoming a rather long and straining code as I add more labels.

    Please Login or Register  to view this content.

  3. #3
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,641

    Re: Dynamically change the color of all labels on a worksheet

    Hi there,

    Try using the following code to overcome the immediate problem:

    Please Login or Register  to view this content.
    Hope this helps - please let me know how you get on.

    Regards,

    Greg M

  4. #4
    Registered User
    Join Date
    09-04-2014
    Location
    WA, USA
    MS-Off Ver
    2007
    Posts
    69

    Re: Dynamically change the color of all labels on a worksheet

    Thanks Greg, this solved my immediate problem!

    My overall goal is to make the selected label one color and all other labels another color ever time a label is selected. I have a cell that stores the number of the selected label. I wouldn't turn down some advice if you had any tips.

    Thanks again.

  5. #5
    Registered User
    Join Date
    09-04-2014
    Location
    WA, USA
    MS-Off Ver
    2007
    Posts
    69

    Re: Dynamically change the color of all labels on a worksheet

    I decided on using this code:

    Please Login or Register  to view this content.
    It seems to work alright. I at least don't ever have to add any code to it after I add new labels. Any tips on making it faster would be appreciated!

  6. #6
    Forum Expert
    Join Date
    06-18-2004
    Location
    Canada
    MS-Off Ver
    Office 2016
    Posts
    1,474

    Re: Dynamically change the color of all labels on a worksheet

    Maybe something like this...

    Please Login or Register  to view this content.
    You'll notice that it exits the sub if the target cell is not A4. You'll also notice that error handling has been added, in case a number is entered that doesn't correspond with one of the labels.

    Hope this helps!

  7. #7
    Registered User
    Join Date
    09-04-2014
    Location
    WA, USA
    MS-Off Ver
    2007
    Posts
    69

    Re: Dynamically change the color of all labels on a worksheet

    Hi Domenic. The exit sub code greatly helps since it reduces the frequency that the code is executed. Thank you.

    I don't need the error handling since a number is only entered into the cell corresponding to a label click event (according to the label number).

    Thanks again!

  8. #8
    Forum Expert
    Join Date
    06-18-2004
    Location
    Canada
    MS-Off Ver
    Office 2016
    Posts
    1,474

    Re: Dynamically change the color of all labels on a worksheet

    That's great, I'm glad I could help.

    Cheers!

  9. #9
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,641

    Re: Dynamically change the color of all labels on a worksheet

    Hi again,

    Try using the attached code in order to reduce the execution time:

    Please Login or Register  to view this content.
    It works by storing the number of the previously selected (highlighted) Label and therefore needs to reset the background colour of only that label whenever a new label is selected.

    I don't have access to all of your code, but it might be possible to reset the previously-selected label (whose number is stored in Cell A4) BEFORE you update that cell with the number of the newly-selected label.

    Hope this helps - please let me know how you get on.

    Regards,

    Greg M

  10. #10
    Registered User
    Join Date
    09-04-2014
    Location
    WA, USA
    MS-Off Ver
    2007
    Posts
    69

    Re: Dynamically change the color of all labels on a worksheet

    Below is the solution I came up with with great thanks to both of you! Gregg, thank you especially for coming back for a 2nd round to help even further. Your idea to store the previously highlighted Label value was such an easy solution that I didn't think of.

    On a side note: I don't know if your constants are a potential substitute for storing data instead of using a cell, but if they are, then I would love to consider them. I have tried to understand how to use them and I am not quite certain how and if they work like I need.

    Thanks again Gregg and Domenic! Let me know if there is anything I can do to repay you for your generous help. I left a positive rep for you.

    Regards, SocratesJC


    Please Login or Register  to view this content.

  11. #11
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,641

    Re: Dynamically change the color of all labels on a worksheet

    Hi again,

    Many thanks for your feedback - I'm glad that I was able to help.

    Regarding my use of Constants, they are what the name suggests, i.e. constants - they are assigned a value only once, and may not be altered subsequently.

    I always use constants for anything associated with e.g. the location of a cell on a worksheet. If you look at your previous code you'll see that you use the address of Cell(A4) in three separate places - this means that if you ever need to use a cell other than Cell(A4) to store information, you'll need to change this cell address three times. Similarly you use a reference to Cell(A7) in two separate locations - so two changes would be required if you ever needed to use another cell to store information.

    Defining the cell locations as Constants (e.g. sCURRENT_LABEL_CELL) allows you to accommodate possible future changes by altering values of the relevant Constants, i.e. in a single location only.

    Hope the above gives you some information regarding the advantage of using Constants wherever appropriate.

    Now, as always in programming, there is more than one way to skin a cat, so I've attached a workbook which (I think!) does what you need, but requires only one "storage" cell instead of the two that are being used at present. This cell stores the Name of the label rather than just the digital suffix of the Name. It also uses only the Label_Click event, and the Worksheet_Change event is no longer required. This approach might be useful either for this project or for a future one.

    Hope this helps - as always, please let me know how you get on.

    Best regards,

    Greg M
    Attached Files Attached Files
    Last edited by Greg M; 11-30-2014 at 07:26 PM. Reason: Minor typo corrected

  12. #12
    Registered User
    Join Date
    09-04-2014
    Location
    WA, USA
    MS-Off Ver
    2007
    Posts
    69

    Re: Dynamically change the color of all labels on a worksheet

    Thanks Greg, I feel the need to think up another way to say thanks before the word gets worn out.

    I tested your workbook and it appears to ardently outperform my own. I am going to take a shot at implementing it into my project via copy-paste. I am confident I can manage to tweak/edit it sufficiently to make it work. It might take me a while longer to fully understand it enough to reproduce it entirely from scratch on my own in the future.

    It is a shame I don't have a nice giant bank-roll. If I did I would hire you in an instant Greg.

    Kind regards,
    SocratesJC

    Ps- Is it against any forum rules to exchange emails?

  13. #13
    Registered User
    Join Date
    09-04-2014
    Location
    WA, USA
    MS-Off Ver
    2007
    Posts
    69

    Re: Dynamically change the color of all labels on a worksheet

    Gregg, I implemented your code and had a grave realization. It did work and all... and I think it probably works better (is there a way officially test this? Run-time?). However, I had completely forgotten that many other subroutines and codes in my project depend on the value in A4. I need the number since it corresponds with things other than just the labels. Any suggestions? For right now I need to revert back (unfortunately) to my old code.

    I certainly don't expect you to spend time looking through it, but I went ahead and posted my whole project for you to look at just in case it helps. Please excuse my sloppy code; I am a 4-month-old developer and I learned code faster than I learned house-keeping.

    Regards,
    SocratesJC
    Attached Files Attached Files
    Last edited by SocratesJC; 11-30-2014 at 05:17 PM.

  14. #14
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,641

    Re: Dynamically change the color of all labels on a worksheet

    Hi again,

    Many thanks for your kind words

    I've taken a quick look at your workbook and it's certainly impressive from someone who's been using VBA for only four months!

    The attached workbook is a slightly modified version of the last one I posted - as per your original requirement, it stores only the digital suffix of the selected Label, so that value will be available for any of your other routines which require it.

    Take a look at it and see what you think - let me know if you need any further information.

    Best regards,

    Greg M
    Attached Files Attached Files

  15. #15
    Registered User
    Join Date
    09-04-2014
    Location
    WA, USA
    MS-Off Ver
    2007
    Posts
    69

    Re: Dynamically change the color of all labels on a worksheet

    Greg,

    Thank you for your compliments; I have been working very hard since the summer trying to teach myself.

    The workbook successfully stores only the suffix, but the color change is not occurring.

    Regards,

    Brandon

  16. #16
    Registered User
    Join Date
    09-04-2014
    Location
    WA, USA
    MS-Off Ver
    2007
    Posts
    69

    Re: Dynamically change the color of all labels on a worksheet

    Hi again,

    It was a simple fix: one of the lines of codes was set to comment...

    Please Login or Register  to view this content.

    Fixed.

  17. #17
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,641

    Re: Dynamically change the color of all labels on a worksheet

    Hi again Brandon,

    Sorry for the delay in replying - things have been a bit hectic at this end.

    Apologies!!! I had "commented-out" a line of code while I was testing it and forgot to "uncomment" it afterwards

    The culprit is highlighted below - just remove the apostrophe at the start of the line and everything should then work correctly.

    Please Login or Register  to view this content.
    Sorry about that - please let me know if you require any further information.

    Regards,

    Greg M


    P.S. Just saw your last post which showed that you'd already found where the error was located. I hadn't realised that the thread had scrolled to a second page until I posted this reply!
    Last edited by Greg M; 12-02-2014 at 04:54 PM. Reason: P.S. added

  18. #18
    Registered User
    Join Date
    09-04-2014
    Location
    WA, USA
    MS-Off Ver
    2007
    Posts
    69

    Re: Dynamically change the color of all labels on a worksheet

    Thanks Greg,

    The code above is just wonderful. I have been very happy with the speed it is operating at. Once more, thank you Greg.

  19. #19
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,641

    Re: Dynamically change the color of all labels on a worksheet

    Hi again,

    Many thanks for all of your feedback - I'm really pleased to hear that everything is working correctly (and quickly ) at your end.

    Best regards,

    Greg M

+ 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. Can I change the color of labels in a 3D pie chart to match cell colors?
    By Macphersonian in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-01-2013, 12:07 PM
  2. Replies: 1
    Last Post: 06-26-2012, 10:02 AM
  3. How to change the color of default letters in the labels?
    By qhu@downstate.edu in forum Excel General
    Replies: 6
    Last Post: 07-26-2006, 10:10 AM
  4. Dynamically change cell range in formula by background color?
    By albert@netmation.com in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-09-2006, 05:15 PM
  5. Change a cell's fill color dynamically?
    By Arlen in forum Excel General
    Replies: 2
    Last Post: 01-22-2005, 06:06 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