+ Reply to Thread
Results 1 to 26 of 26

Changing cell background color/borders function

  1. #1
    Registered User
    Join Date
    03-26-2016
    Location
    cz
    MS-Off Ver
    07/10
    Posts
    73

    Changing cell background color/borders function

    Hello, please keep it simple, i am a complete noob

    i would like to have a function that works something like this


    {name of the function} ( {adress of a cell containing a color i want to use} ) {cells that will be colored}

    simply put, the function copies a backround color of one cell to selected cells no matter what the values of those cells are

    i would use this function to see the shift of different people (colors) on a time table.
    i could use for example an IF function to color a certain range when i write pick a 1 hour shift, and i could color a bigger range when i pick a 2 hour shift


    It seems that changing the backround color is complicated but it would work if i could at least change the bordest with the same logic.. something like

    {name of the function} ( {adress of a cell containing a color i want to use} {adress containing thickness of borders ) {cells that will be colored}


    -------------------------------------------------------------------------

    here is what i have got so far


    This changes the color of a cell defined by user - but only to a color predefined in the code, all i would need (i think) is to somehow be able to define that color by a cell (either color of the cell or numbers, i could make it work but id prefer the color directly to be read from a pre-colored cell > it should be possible to change and update all the colors)

    -----------------------------
    Function SetIt(RefCell)

    RefCell.Parent.Evaluate "getRGB(" & RefCell.Address(False, False) & ")"
    SetIt = ""
    End Function

    Sub getRGB(RefCell As Range)
    RefCell.Interior.Color = 123456 ***this is the color number, i would like to be able to change
    End Sub

    -----------------------------

    I found this online and it works very well but i cant define the color - i tried putting the numers of rgb there as variables R G and B, that didnt work, i dont really have any idea how to put variables in there.. "noob" id rpefer it to read the color directly anyway..
    i also found a piece of code that takes a cells color and writed down its RGB value, but i cant put them together due to lack of my skill

    Thanks in advance for the help

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

    Re: Changing cell background color/borders function

    Hi there,

    Sorry, but the ONLY thing that a User-defined function can change is the VALUE displayed in the cell where the UDF is entered.

    If you want to change anything else, you'll have to use a subroutine.

    Hope this helps.

    Regards,

    Greg M

  3. #3
    Registered User
    Join Date
    03-26-2016
    Location
    cz
    MS-Off Ver
    07/10
    Posts
    73

    Re: Changing cell background color/borders function

    But this function i provided succesfully changes a color, i just want to define which color

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

    Re: Changing cell background color/borders function

    Hi again,

    Many thanks for this very interesting workaround.

    When I hear "How do I change ... " and "Function" mentioned in the same sentence I tend to switch off and give the reply I gave you originally, but your code shows that you CAN teach an old dog new tricks!

    See if the following version of your code does what you need:

    Please Login or Register  to view this content.

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

    Regards,

    Greg M
    Last edited by Greg M; 03-27-2016 at 11:30 AM. Reason: Minor change

  5. #5
    Registered User
    Join Date
    03-26-2016
    Location
    cz
    MS-Off Ver
    07/10
    Posts
    73

    Re: Changing cell background color/borders function

    i gave it a quick test and it works great!

    i couldnt understand the syntax to be able to do it myself but i knew you could define the color somehow!

    ive seen a lot of people ask for this function too as i was looking for solutions but they mostly gave up on it (too soon!)


    ONE more addition, if possible, could you make it ctrl+alt+F9 itself somehow, so it works dynamically when i change the source color? or maybe there are better solutions too, im definately going to use this often, just gonna change the functions name, i prefer shorter names

    i tried it out on droplists as well and with text and different styles, it doesnt seem to glitch outside of what i mentioned, it also updates the color once you retype something inside any of the target cells, maybe someone would make a use of that

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

    Re: Changing cell background color/borders function

    Hi again,

    Many thanks for your feedback and also for the Reputation increase - much appreciated

    Regarding:


    ONE more addition, if possible, could you make it ctrl+alt+F9 itself somehow, so it works dynamically when i change the source color?

    I don't think this is possible. Even making the function Volatile (never a good idea anyway!) would only cause it to recalculate on the basis of its argument - as the argument is a Cell (not a cell background colour), no recalculation would be triggered when the source cell background colour changes.


    The best I can suggest is to run the following routine whenever you change the background colour of one or more source cells:

    Please Login or Register  to view this content.
    You can change the highlighted value to suit your newly-renamed Function.


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

    Regards,

    Greg M

  7. #7
    Registered User
    Join Date
    03-26-2016
    Location
    cz
    MS-Off Ver
    07/10
    Posts
    73

    Re: Changing cell background color/borders function

    So this formula checks cells with the background function for changes? where should i put it, in the sheet or in the module or perhaps in the background functions module?

  8. #8
    Registered User
    Join Date
    03-26-2016
    Location
    cz
    MS-Off Ver
    07/10
    Posts
    73

    Re: Changing cell background color/borders function

    WAIT, i used the background function the way i planned to and it works very well, the colors update due to the IF function i use it with.
    or perhaps i forgot to delete the mod you just gave me XD

    i want you to také a look, its the last sheet called sheet2, this is my random file so theres a lot of unnecesary mess, sorry for that, there are also two sheets with this name but youll figure

    it shows a time Schedule where you choose name and time, it then colors the time accordingly - next to it there is an orange box showing the IF code i made, také a look.
    Is there an easier way to make this work? the code seems to recopyable but i feel like it is not very good, since eventually there are gonna be more names (at least 7) and i have to type each variability each time XD

    also it has one buug id love you to help me solve

    when you choose 1,5 hours it colors the right cells, but when you then decide to choose 0,5 instead, it doesnt delete them anymore

    i was thinking deleting them each time, but then if there was a time assigned in the next hour it would delete that too - well it is fine since its go back after refreshing, but the other color still remains

    for example, i chose orange for 0,5 in the third column, then i chose 2 hours in green in the first column "accidentally" then i changed back it to 0,5

    now everyone would think theres no free space, take a look and thank you for the help!

    (my attempt of solving it is in the yellow cell, it works but it would delete the whole thing each time and that is unconvenient)

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

    Re: Changing cell background color/borders function

    Errmmm ... I think you forgot to attach the workbook!

  10. #10
    Registered User
    Join Date
    03-26-2016
    Location
    cz
    MS-Off Ver
    07/10
    Posts
    73

    Re: Changing cell background color/borders function

    Not again xD, i keep forgetting to press the upload button
    Attached Files Attached Files

  11. #11
    Registered User
    Join Date
    03-26-2016
    Location
    cz
    MS-Off Ver
    07/10
    Posts
    73

    Re: Changing cell background color/borders function

    ive been playing with it and i rememebr trying a different if false function at the end of the two lines.. the upper one stays the same it is if cleared, the lower one deletes itself - and also others if they were in range, which is the reason i cant use that

    i figured simply selecting the whole thing, clearing the color to no fill and then pressing the refresh button works the best, how would the line for "replace selected area with no fill" be, i could just add something like it to the refresh button

  12. #12
    Registered User
    Join Date
    03-26-2016
    Location
    cz
    MS-Off Ver
    07/10
    Posts
    73

    Re: Changing cell background color/borders function

    another bug found - the function doesnt work when the target and source cells are from different sheets
    not sure why, it is possible its because of the workaround

  13. #13
    Registered User
    Join Date
    03-26-2016
    Location
    cz
    MS-Off Ver
    07/10
    Posts
    73

    Re: Changing cell background color/borders function

    another bug found - the function doesnt work when the target and source cells are from different sheets
    not sure why, it is possible its because of the workaround

    Its very important for me that it can do that ...
    Last edited by tigfur; 03-29-2016 at 10:11 AM.

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

    Re: Changing cell background color/borders function

    Hi again,


    another bug found - the function doesnt work when the target and source cells are from different sheets
    not sure why, it is possible its because of the workaround

    Its very important for me that it can do that ...


    No, it's not related to the workaround - to achieve this you need to modify the original code as follows:

    Please Login or Register  to view this content.

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

    Regards,

    Greg M
    Last edited by Greg M; 03-29-2016 at 06:46 PM. Reason: Minor change

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

    Re: Changing cell background color/borders function

    Hi again,

    Sorry, but as far as your Post #8 is concerned, I really can't understand what's supposed to be happening

    Regards,

    Greg M

  16. #16
    Registered User
    Join Date
    03-26-2016
    Location
    cz
    MS-Off Ver
    07/10
    Posts
    73

    Re: Changing cell background color/borders function

    im thinking about opening a new thread for that part but i feel like its simple, i just dont know what function to use...

    -tried to add your changes to the code - didnt work
    copied the whole code - worked (lol not sure why, it looked the same)
    i have tried to change the name after, do i have to change every word background to the new color or only those that are only "background" ?


    anyway, back to the 8) problem

    in the orange boxes there are IF functions combined with this one

    next to it is a time Schedule

    in the Schedule there are two drop lists - of people and time

    the function checks what the person and time is and colors the right color (people) to the right area (time)

    i would just like to somehow simplify that, only using IF doesnt seem very good, there have to be better formulas for this task

    something like "if THIS(1) cell equals one of THOSE (1,2,3,4,5,6) cells, use the color of cell that equals from THOSE cells (1) to color THIS(1) cell" this way i could define it to take the color in just one line, because for now i have to define each name and area combination on its own - of course i would have to define the are still but thats simple


    EDIT: here is what i am working on, there is a lot of things extra, some of them i know how to easily deal with, some of them not so much, you might get a little lost in it, but i mainly wanna simplify the problem i mentioned above

    maybe itll help - i might ask some stuff later on this forum if i find another problem, this place is very helpful

    also what is missing in the file, i originally planned on making those names clickable buttons that would assign a color to selected area, but since we have this new awesome function that isnt necesary, though two buttons would be neat, one with a certain color for assigning lunch breaks, and one to "clean up" so a "no fill" color

    that reminds me, we havent quiet solved the refreshing of the cells

    EDIT: i have an idea for assigning lunch so it doesnt have to be done by hand - now if we change the color of the whole sheet to "no fill" and then refresh, all the colors done by a function will re-appear, leaving the falsely coloured gone.
    so, how is the command for "set THIS AREA to "no fill" that i could add to the button macro, i think the area will be the same in every sheet (ill copy this for every day of the month) so there shouldnt be any problem with it...

    i only tested this in the previous file
    i know i ask too much stuff at once XD i just have so little time lately
    Attached Files Attached Files
    Last edited by tigfur; 03-30-2016 at 02:51 AM.

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

    Re: Changing cell background color/borders function

    Hi again,


    -tried to add your changes to the code - didnt work
    copied the whole code - worked (lol not sure why, it looked the same)

    It's very definitely NOT the same! The latest version includes the name of the worksheet which contains the source colour cell.

    The attached workbook appears to work correctly in that cell colours on Sheet1 are copied to the appropriate cells on Sheet2.


    I haven't time to look at the remaining points in your last post but I'll see what I can do later on.


    Regards,

    Greg M
    Attached Files Attached Files

  18. #18
    Registered User
    Join Date
    03-26-2016
    Location
    cz
    MS-Off Ver
    07/10
    Posts
    73

    Re: Changing cell background color/borders function

    also the coloring is only in the first cell, i plan on copying it later but i feel like the slow computer at work already has trouble with it, hopefully some autosave function will save it every three minutes lol
    Attached Files Attached Files

  19. #19
    Registered User
    Join Date
    03-26-2016
    Location
    cz
    MS-Off Ver
    07/10
    Posts
    73

    Re: Changing cell background color/borders function

    HELP
    there was a bug but i really couldnt name it, it just coloured stuff randomly
    but now i realized the colors have the same pattern as the colors on the other side!
    when i double click in the name 6 or lower in the settings tab, or somewhere around - no need to even type anything, the colors of this sheet update to the same colors as the other sheet!! thus sometimes changing the color names as well and so on.
    At first i thought that would only be because i didnt name the sheet in the function but i tried naming it and it didnt work, so the only thing left i think is this function, did we maybe miss something?
    Try it out yourself im attaching the file - go to setting tab and double click name6!

    This is a major bug and its keeping me from finishing this project right now. I cant find what causes it

    EDIT: Also it keeps crashing all the time, i noticed in my advanced version that i have kept a copy of your code in each sheet and the workbook, i deleted those, but nothing changed, it still crashes often and the colors are still changing

    EDIT 2) Tried to press ctrl + alt + F9 on a completely new sheet and it also colored that sheet the same way, it definately has to be something about the function but i cant tell what it is

    EDIT 3) tried only the function in a new file, had the same problem, so the function is wrong, damn i really liked it
    Attached Files Attached Files
    Last edited by tigfur; 04-02-2016 at 12:21 PM.

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

    Re: Changing cell background color/borders function

    Hi again,

    Using the function on different worksheets definitely makes life more complicated!

    The following version of the function seems to work in the attached workbook:

    Please Login or Register  to view this content.
    Regards,

    Greg M
    Attached Files Attached Files

  21. #21
    Registered User
    Join Date
    03-26-2016
    Location
    cz
    MS-Off Ver
    07/10
    Posts
    73

    Re: Changing cell background color/borders function

    a quick press of ctrl alt and F9 in the file you added proved that to be wrong

  22. #22
    Registered User
    Join Date
    03-26-2016
    Location
    cz
    MS-Off Ver
    07/10
    Posts
    73

    Re: Changing cell background color/borders function

    Could be due to the undeleted functions left in the book i sent you, but in my new file after refreshing and changing the function nothing happened, thats a good sign!

    Seems that it works with no problem after all!

    Good job! Im gonna rank it solved then and anybody who wants a function like this can see it, no need for "functions cant change formatting" anymore.

    EDIT.
    So far so good! Also my file stopped crashing, or at least hasnt crashed in the few minutes ive been using it, before the change i just needed to select a cell and copy it for it to crash!

    I Think you should take the credit and perhaps upgrade it so there would be a function for borders or so, just a little project for fun, someone would find a use for it, i really love this function.

    BTW take a look at the very right, have you seen the IF function using this? it works but i feel like it could be done better LOL Im gonna post it for fun (i hate that you cant ctrl + A to select text inside a cell and CTRL + D to deselect like im used from photoshop but thats my problem :D )

    Please Login or Register  to view this content.
    Last edited by tigfur; 04-02-2016 at 02:58 PM.

  23. #23
    Registered User
    Join Date
    03-26-2016
    Location
    cz
    MS-Off Ver
    07/10
    Posts
    73

    Re: Changing cell background color/borders function

    Nope, false alert, still crashes, less often but it still does.

    I really wanna figure out why, could it be because of this fuunction? after all its in about 150 cells. or maybe the checkboxes but i havent found anything better than those...

    its the last thing keeping me from using it

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

    Re: Changing cell background color/borders function

    Hi again,

    I haven't found any situation which causes the workbook to crash, but perhaps I'm not experimenting with it as much as you are.


    Regarding:


    BTW take a look at the very right, have you seen the IF function using this? it works but i feel like it could be done better LOL

    I'm sorry, but I wouldn't even look at a formula that contains 32 nested IF's


    Regards,

    Greg M

  25. #25
    Registered User
    Join Date
    03-26-2016
    Location
    cz
    MS-Off Ver
    07/10
    Posts
    73

    Re: Changing cell background color/borders function

    haha, i need to find a way to replace it with some easier formula, havent found it yet, also ever since the update of your formula it crashes less, ill see what i can do once i find what causes it.

  26. #26
    Registered User
    Join Date
    03-26-2016
    Location
    cz
    MS-Off Ver
    07/10
    Posts
    73

    Re: Changing cell background color/borders function

    HEYS! i just found an amazing bug!

    It generally does what it did before (it copied colors from sheet to sheet) but with FILES

    i copied a filled time table and kept working on one copy while deleting the other one to have a blank for the next day, then i pressed the "refresh" button and suddenly the colors from one file appeared in the other blank file while i had both of them open.

    it doesnt happen when i only have one file open but i though its really interesting!

    perhaps the vbas somehow mix it up since all the names are the same?

    EDIT: I do need it solved to prevent confusion
    Last edited by tigfur; 04-17-2016 at 04:34 AM.

+ 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. Apply background color and maintain default borders
    By Carson Dyle in forum Excel General
    Replies: 3
    Last Post: 05-18-2013, 04:42 PM
  2. [SOLVED] changing cell values of a column based on its background color
    By liderplaza in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-17-2012, 11:08 AM
  3. Changing cell background color based on date
    By tvwhome in forum Excel General
    Replies: 7
    Last Post: 01-22-2011, 03:56 AM
  4. Changing cell background color automatically
    By PCMagician in forum Excel General
    Replies: 4
    Last Post: 03-11-2008, 12:58 PM
  5. Changing background color based on different cell
    By djarcadian in forum Excel General
    Replies: 3
    Last Post: 08-10-2006, 05:44 PM
  6. [SOLVED] Macro for Changing Cell Background Color
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 12-02-2005, 01:45 PM
  7. automatically changing the background color of a cell
    By martin in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 01-06-2005, 08:06 AM

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