+ Reply to Thread
Results 1 to 29 of 29

Can a code work in only ONE worksheet?

  1. #1
    Registered User
    Join Date
    04-08-2013
    Location
    Fresno, CA
    MS-Off Ver
    Excel 2007
    Posts
    14

    Can a code work in only ONE worksheet?

    Hello, I have the following code in Sheet1 that is supposed to auto-update the timestamp in cell "J36" when anything has been updated in the sheet(1).

    Please Login or Register  to view this content.
    Also, I have the same code in Sheet3, again, to do the same - auto-update the timestamp in cell "H36" when anything on that particular sheet(3) has been updated

    Please Login or Register  to view this content.
    The codes work, however, if I update Sheet1, then the timestamp in both Sheet1 and Sheet3 auto-updates, and same for when I edit Sheet3 - timestamp updates on both sheets. My question is - how can I get the codes to only work for the sheet that they are on? For example, when I update Sheet1, I just want the timestamp on Sheet1 to update, not on both.

    Thank you.
    Last edited by DoctorMorty; 04-08-2013 at 02:35 PM.

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

    Re: Can a code work in only ONE worksheet?

    there is nothing in those codes that would affect any sheet other than the one containing each. something must be triggering a change on the other sheet
    Josie

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

  3. #3
    Registered User
    Join Date
    04-08-2013
    Location
    Fresno, CA
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Can a code work in only ONE worksheet?

    I do have a code on Sheet2 to update multiple pivot tables all at once by clicking on a button.

    Please Login or Register  to view this content.
    Would this have anything to do with it? Otherwise, this is it, 3 sheets, total, and that's all the macros in the workbook.

    Thank you.

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

    Re: Can a code work in only ONE worksheet?

    if you run that and there are pivots on each sheet then both change events will be triggered

  5. #5
    Registered User
    Join Date
    04-08-2013
    Location
    Fresno, CA
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Can a code work in only ONE worksheet?

    I tried taking out the pivot table code to no avail, timestamp still updated on both Sheet1 and Sheet3. And no, the pivot tables are only on Sheet2 (it's grabbing info from Sheet1). Soooo....

    Sheet1 = autoupdate timestamp macro
    Sheet2 = pivot table macro
    Sheet3 = autoupdate timestamp macro

    Let me know if you need more info, otherwise I'm a beginner and thought I was keeping it simple enough for my first time with macros.

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

    Re: Can a code work in only ONE worksheet?

    I'd need to see the workbook-the posted code does not explain that behavior (unless you have both sheets selected!)

  7. #7
    Registered User
    Join Date
    04-08-2013
    Location
    Fresno, CA
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Can a code work in only ONE worksheet?

    Oh man, I was hoping you wouldn't ask to see it, lol, it has confidential info on it =/

    Okay, I'm hoping this will help you [help me], as I cleared the contents, but left the formatting and codes, and whatnot.
    Attached Files Attached Files

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

    Re: Can a code work in only ONE worksheet?

    I'm afraid I am not seeing the behavior you describe-if I change a value on one sheet only that sheet's date is updated

  9. #9
    Registered User
    Join Date
    04-08-2013
    Location
    Fresno, CA
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Can a code work in only ONE worksheet?

    That's the other thing I was afraid you'd say. However, I did notice one thing yesterday after taking off and putting back on the pivot table code, the timestamp on Sheet3 now autoupdates upon opening and closing of the workbook.

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

    Re: Can a code work in only ONE worksheet?

    do your pivot tables update automatically when the workbook is opened?

  11. #11
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Can a code work in only ONE worksheet?

    Hi

    Sorry to intrude.

    I sometimes declare a Public changeflag and use that to ensure my code only runs when I expect it to run.

    Could you not do something similar.

    in your scenario I would use something like:

    Please Login or Register  to view this content.

  12. #12
    Registered User
    Join Date
    04-08-2013
    Location
    Fresno, CA
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Can a code work in only ONE worksheet?

    Negative; I created the button so that all pivot tables are updated at once as opposed to "refreshing" each table one-by-one. But no, I did not see that the tables were being updated automatically, just the timestamp on Sheet3 upon opening and closing the workbook. It seems we have taken a step back.

  13. #13
    Registered User
    Join Date
    04-08-2013
    Location
    Fresno, CA
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Can a code work in only ONE worksheet?

    No apologies, all input is welcomed.

    What does the code do and where should I put it?

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

    Re: Can a code work in only ONE worksheet?

    did you remove any controls from any sheets?

  15. #15
    Registered User
    Join Date
    04-08-2013
    Location
    Fresno, CA
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Can a code work in only ONE worksheet?

    Quote Originally Posted by JosephP View Post
    did you remove any controls from any sheets?
    Since I copied and pasted those codes into my sheets (only modifications I did was the password on the pivot table code and the targeted cells in the timestamp codes), I'm 100% sure I didn't do anything else, as I'm just a beginner in Excel

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

    Re: Can a code work in only ONE worksheet?

    do you see the same behavior with the sample file you posted?

  17. #17
    Registered User
    Join Date
    04-08-2013
    Location
    Fresno, CA
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Can a code work in only ONE worksheet?

    Quote Originally Posted by JosephP View Post
    do you see the same behavior with the sample file you posted?
    Okay, this is very, very strange. In the same file I attached, I was able to input/edit Sheet1 and the timestamp changed in just the Sheet1 and did not update Sheet3. I waited a few minutes to update Sheet3, and when I did, the timestamp updated in just that sheet alone, leaving Sheet1 alone.

    Other than clearing the contents in the entire workbook, I am not sure why it would be working the way I would like for it to work.

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

    Re: Can a code work in only ONE worksheet?

    are you sure you didn't have multiple sheets selected? (you would see [group] in the title bar)

  19. #19
    Registered User
    Join Date
    04-08-2013
    Location
    Fresno, CA
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Can a code work in only ONE worksheet?

    Quote Originally Posted by JosephP View Post
    are you sure you didn't have multiple sheets selected? (you would see [group] in the title bar)
    Pretty sure, Joseph, pretty sure.

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

    Re: Can a code work in only ONE worksheet?

    does the original still exhibit the problem?

  21. #21
    Registered User
    Join Date
    04-08-2013
    Location
    Fresno, CA
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Can a code work in only ONE worksheet?

    Quote Originally Posted by JosephP View Post
    does the original still exhibit the problem?
    Hm, it works just fine. I don't know what to say, Joseph, been researching for a week, only when I finally asked the question, it works.

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

    Re: Can a code work in only ONE worksheet?

    could be excel gremlins-does happen now and then

  23. #23
    Registered User
    Join Date
    04-08-2013
    Location
    Fresno, CA
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Can a code work in only ONE worksheet?

    Thanks Joseph, for your help!

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

    Re: Can a code work in only ONE worksheet?

    you're welcome :-)

    if the problem starts again let us know

  25. #25
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Can a code work in only ONE worksheet?

    Hi

    This code would go at the top of each macro.
    To check which sheet is calling the macro. and if it isn't the right one it jumps to row 100 and exits.


    Please Login or Register  to view this content.

    This code goes at the bottom of your macro just above "End Sub"

    Please Login or Register  to view this content.

  26. #26
    Registered User
    Join Date
    04-08-2013
    Location
    Fresno, CA
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Can a code work in only ONE worksheet?

    Joseph/mehmetcik/everyone

    I think I figured it out...

    It's not so much the codes, themselves, it was so much the order in which I was putting the codes in the entire workbook.

    In another copy of the workbook, with no codes, I edited it by inputting the pivot table code first, then the auto timestamp codes next (first in Sheet1 then Sheet3). Both timestamps were autoupdating upon opening and closing of the workbook, which clearly was not the goal. SOOO...

    I deleted all the codes and the "=NOW()" function in the sheets, saved the workbook, then one by one, inputted the auto update timestamp code, THEN the "=Now()" function in Sheet1, saved it, tested it, and it worked just fine. Then, again, inputted the code for Sheet3, inputted the "=NOW()" function, saved it, tested it, and boom - worked, and without autoupdating the timestamp in Sheet1. So then finally, inputted the code to update the pivot tables, saved it, tested it, and boom boom boom, the entire workbook is working to the way I wanted it to work.

    So again, thank you both for your inputs! I think maybe this is something to consider when working with different codes in a single workbook in the future, who knows?!

    Anyhoot, thanks again!

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

    Re: Can a code work in only ONE worksheet?

    this is the first time you mentioned an =now() formula-that is volatile and will update whenever the workbook calculates for any reason as well as when it opens (the code does not use a formula). it sounds as though that is the cause of your behavior

  28. #28
    Registered User
    Join Date
    04-08-2013
    Location
    Fresno, CA
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Can a code work in only ONE worksheet?

    Interesting. I checked the workbook just now, and the timestamps on both sheets did not update to the current time, which is good. Which means I'll just leave it, for now, and if I'm having issues, I know where to look. Thanks Joseph.

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

    Re: Can a code work in only ONE worksheet?

    if you put the codes in the sheet before entering the =now() formula then the code will overwrite the formula with a static timestamp immediately after you enter it. if you already had the formula in place before entering the code then the code would not overwrite it until you entered some other data to trigger the change event but the formula would update itself automatically with each calculation until then

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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