+ Reply to Thread
Results 1 to 22 of 22

Add user-based color functionality to macro that tracks changes

  1. #1
    Registered User
    Join Date
    07-22-2010
    Location
    New York
    MS-Off Ver
    Excel 2007
    Posts
    12

    Add user-based color functionality to macro that tracks changes

    Hello,

    I was hoping someone could help me add another feature to a macro. Currently, the macro helps track changes in a worksheet by highlighting the changed cell and automatically logging certain data about the change on another worksheet.

    Is it possible to have the macro use a different color based on the user making the edits. For example, if user A edits a cell, it turns green, user B edits a cell, it turns red and so on.

    Here is the macro as I am using it:

    Please Login or Register  to view this content.
    How do I add to it for the functionality I described?

    Thanks in advance for your help.
    Last edited by jmalankar; 08-03-2010 at 05:35 PM.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,366

    Re: Add user-based color functionality to macro that tracks changes

    It looks like you have all the information that you need. I'd suggest that you set up separate sheet and list the user names and the colour code you want for each user in a table. Then use the user name to look up the table and find the colour. Plug the colour into your code.

    Regards

  3. #3
    Registered User
    Join Date
    07-22-2010
    Location
    New York
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Add user-based color functionality to macro that tracks changes

    Sorry, I think I was unclear in my explanation. Based on my understanding of your suggestion, that would help me color code the worksheet with the log of all the changes.

    I was actually looking to have the cells on the original worksheet change colors based on the user that makes the changes. This would be separate from what happens on the log worksheet.

    Did I understand your suggestion correctly?

    By the way, I did not write the original macro. I actually found it on another thread, so I'm not too familiar with VBA. Any code that you could share would be much appreciated.

    Thanks for your help.

  4. #4
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: Add user-based color functionality to macro that tracks changes

    Hi jmalankar;

    You misunderstood TMShucks.

    On a separate sheet put every user's name
    Please Login or Register  to view this content.
    Then in your macro: (This isn't the actual code you would use. It's just to show you the concept).
    Please Login or Register  to view this content.
    Foxguy

    Remember to mark your questions [Solved] and rate the answer(s)
    Forum Rules are Here

  5. #5
    Registered User
    Join Date
    07-22-2010
    Location
    New York
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Add user-based color functionality to macro that tracks changes

    Hello all,

    Thanks for the quick responses. Sorry for my slow reply, just got caught up with work stuff.

    I tried to enter the code that foxguy suggested. Unfortunately, I'm very new to macros and don't know too much about VBA, so I wasn't able to fill in the gaps.

    Here is the code that I tried to use:


    Please Login or Register  to view this content.
    I have also attached a test file. Hopefully that will help you see how I have set it up. If possible, it would be great if someone could show me how to write the code for the color functionality.

    Thanks so much for all your help. You guys are great!

    JM
    Attached Files Attached Files
    Last edited by jmalankar; 07-26-2010 at 09:38 AM.

  6. #6
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: Add user-based color functionality to macro that tracks changes

    Hi JM;

    Here's a file that does what you want (I think). Just make sure that the UserName is in Column A on sheet3 with the color that you want.

    I also changed your Worksheet_Change for Sheet1 to make it more efficient. I put several notes in the file so you can improve the next macro you write.

  7. #7
    Registered User
    Join Date
    07-22-2010
    Location
    New York
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Add user-based color functionality to macro that tracks changes

    Hi foxguy,

    So the file you sent me works exactly the way I envisioned, so thank you very much for that.

    I'm now trying to copy the macro into the file that I actually need it for and am running into some issues. Here is the code with adjustments in sheet #'s to match the file:


    Please Login or Register  to view this content.
    I thought I changed all the sheet references to match the worksheets, but I keep getting an error message saying "Run-time error '9': Subscript out of range" when I go to make a change on the worksheet. When I click debug, the following code is highlighted in yellow:

    Please Login or Register  to view this content.
    I thought I had changed the reference properly since the usernames are definitely listed in column A on sheet 4.

    Any idea on why the error is coming up?

    Thanks again for your continued help.

    JM

  8. #8
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: Add user-based color functionality to macro that tracks changes

    Hi JM;

    Sorry it took so long to get back to you. I've been sick in bed all week.

    Subscript out of range on
    Please Login or Register  to view this content.
    is saying that there is no sheet with the tab name "Sheet4" in your workbook. Make sure it's not "Sheet 4" with a space.

    If you have not changed codenames on any of the sheets then, in the Project Explorer the sheet name will be something like this:
    Sheet?(Sheet4)
    If you see this
    Sheet4(SomeOtherName), then you're using the wrong terminology.

    In Sheet4(SomeOtherName) - "Sheet4" is the codename, and "SomeOtherName" is the tab name.
    Please Login or Register  to view this content.
    is looking for a sheet with a tab name of "Sheet4"
    it would actually be better code if you
    Please Login or Register  to view this content.
    Then you can change the tab name all you want and the code will still work. I did not use that code when I wrote it, because it's too hard to explain the difference between Codename and tab Name to people.

  9. #9
    Registered User
    Join Date
    07-22-2010
    Location
    New York
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Add user-based color functionality to macro that tracks changes

    foxguy,

    So I think we're inches away from closing this one out. The color functionality now works and it's great. But now the "Before" column on the log, which showed the cell's previous value, is not populating. Any idea why that might be?

    I have copied the code below for your reference:


    Please Login or Register  to view this content.
    Thank you for your continued help.

    JM

  10. #10
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: Add user-based color functionality to macro that tracks changes

    JM;

    I don't see anywhere in your old code where you were storing old values. And I didn't write anything that stored old values.

    I assume that you're putting the old value into column "G" and the current value into column "H", so change the macro on these lines:
    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    07-22-2010
    Location
    New York
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Add user-based color functionality to macro that tracks changes

    Foxguy,

    I tried pasting in the change that you recommended so that the code now reads like this:


    Please Login or Register  to view this content.
    It actually didn't work at first, but then I moved the line that said ".Cells(lRow, "H").Value = Target.Value" before ".Cells(lRow, "G").Value = .Cells(lRow, "H").Value" instead of after. It worked after that. I'm glad that it's working, but just want to make sure it's working for the right reasons. Was that just because the macro needed to see the one value first?

    Anyhow, I think we're all set here. Thanks so much for all your help!

    JM

  12. #12
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: Add user-based color functionality to macro that tracks changes

    JM;

    No, you're not storing the old value in G, you're storing Target.Value in both G & H.
    Please Login or Register  to view this content.
    You're putting Target.Value into H. Then you're putting H's value (which is now Target.Value) into G.
    Please Login or Register  to view this content.
    Puts the previous H value into G, then puts Target.Value into H.

  13. #13
    Registered User
    Join Date
    07-22-2010
    Location
    New York
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Add user-based color functionality to macro that tracks changes

    Thanks for the explanation. I understand what you said and the log is definitely doing the wrong thing upon second glance. Unfortunately, putting in the suggested code doesn't seem to be working. When I make a change, column H definitely shows the new value that I put in, but column G remains empty.

    Just to confirm, column G shouldn't necessarily have the previous H value, assuming previous means the previous row. There would be cases when changes are made to different cells in random sequence. In those cases, using the previous row would return the wrong "old" value for the edited cell. Does the code know to check the cell reference to make sure it's pulling the right "old" value for column G?

    Any idea why this is happening?

    I have attached a file with the same sheet set up and demonstrated the issue. You'll notice that column G is empty despite making edits to the same cell several times.

    Thanks again for your continued help.

    JM
    Attached Files Attached Files

  14. #14
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: Add user-based color functionality to macro that tracks changes

    There was no code in the file.

  15. #15
    Registered User
    Join Date
    07-22-2010
    Location
    New York
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Add user-based color functionality to macro that tracks changes

    Sorry, didn't work again
    Attached Files Attached Files

  16. #16
    Registered User
    Join Date
    07-22-2010
    Location
    New York
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Add user-based color functionality to macro that tracks changes

    Let's try that one more time.
    Attached Files Attached Files

  17. #17
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: Add user-based color functionality to macro that tracks changes

    Hi JM;

    It just occurred to me. What you're doing won't work. I had it in my head that you would be searching for a Row & Column # in Columns E & F. You're not. You're adding a row. You have to store the Value of the cell you're going to change BEFORE you change it. Once it's changed, there's no way to know what it was (a few exceptions, but this isn't one of them).

    You will have to use the Worksheet_SelectionChange(Target as Range) to store the value of the cell you're moving into, then when it's changed and triggers Worksheet_Change(Target as Range), you pull the value out of wherever it was stored during the Worksheet_SelectionChange().

  18. #18
    Registered User
    Join Date
    07-22-2010
    Location
    New York
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Add user-based color functionality to macro that tracks changes

    Foxguy,

    Interesting development. I'm wondering though if there's anything from the old code that could be used.

    Old code:

    Please Login or Register  to view this content.
    With that code, the before column is populated in the way I would want it to work. My understanding of code is pretty basic though. Can you see what in that code makes the before functionality work? Is there any way to integrate that part with the revised code that you put together?

    If not, would you mind showing me how to use "Worksheet_SelectionChange(Target as Range)." Seeing the code or building on that sample file would be really helpful.

    Thanks so much,

    JM

  19. #19
    Registered User
    Join Date
    07-22-2010
    Location
    New York
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Add user-based color functionality to macro that tracks changes

    Actually I think I figured it out.

    From what I can tell, the original code I put in the last post was creating a mirror worksheet every time a change was made. Then each time a cell was changed the second time, the macro checked the mirror worksheet to find the old value and pasted it in the "Before" column. While this isn't the cleanest solution, it does serve my purposes well enough.

    Here is the code that I put together using the two versions:


    Please Login or Register  to view this content.
    Please let me know if this solution is sustainable and stable.

    Thanks again for your continued help.

    JM

  20. #20
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: Add user-based color functionality to macro that tracks changes

    Other than the fact that your file size will be bigger with the extra sheet, I don't see any reason why it wouldn't work.

    Get rid of the red line:
    Please Login or Register  to view this content.
    You could get rid of the extra sheet by doing a FIND in Sheet3 to find the last time that cell was changed in order to get the last value. That's not as hard as it sounds.

  21. #21
    Registered User
    Join Date
    07-22-2010
    Location
    New York
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Add user-based color functionality to macro that tracks changes

    Thanks for the confirmation, Foxguy.

    Agreed that some kind of FIND method would be cleaner, but I'm eager to have this completed and move on to the next thing.

    Thanks so much for your help throughout this process.

    Is there any particular way to mark this thread as solved aside from typing that?

    SOLVED

    JM

  22. #22
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: Add user-based color functionality to macro that tracks changes

    On Post # 1 of the thread, click "Edit", click "Go Advanced" in the Title box change it to "[SOLVED]"

    And rate the answer by clicking the blue scales in the upper right corner of my answer>

+ 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