+ Reply to Thread
Results 1 to 26 of 26

Worksheet Change Event

  1. #1
    Registered User
    Join Date
    07-20-2010
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    59

    Worksheet Change Event

    Hi all,

    With some very useful help from a forum member I was able to create some code which works well in a separate module. However when I copy and paste it into a sheet (worksheet change) module the code doesn't seem to work.

    Please Login or Register  to view this content.
    Cheers,

    Andrew

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Worksheet Change Event

    Andrew,
    Unless, I am missing something, you do not seem to have a target column, or row which with the intersction with another range could fired the code.
    I think the simplest option (IMO) would be to have your main code on standard module and create another event driven macro,like

    Please Login or Register  to view this content.
    You can easily change the target and range which are the triggerig points for your macro to run.

  3. #3
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967

    Re: Worksheet Change Event

    Does the code fire at all? You can check this by putting a breakpoint on the first line.

    Also, your text says change and the code says SelectionChange. I note that you have some select statements in the code which will mean that the code will fire from within itself which might lead to infinite recursion. It's a good idea to turn events off and then back on again with the Application.EnableEvents statement at the start and end of the code.
    Martin

  4. #4
    Registered User
    Join Date
    07-20-2010
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    59

    Re: Worksheet Change Event

    Thanks AB,

    Yes, I do not seem to understand the target column/row relationship and how to use it in a worksheet change event. What in yr example is range ("F19") referring to?

  5. #5
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Worksheet Change Event

    Andrew,
    In your case it is actaully is not "Change event", rather "Selection Event", i.e your macro will be triggered by selecting a cell, or range, but the underlying rules are the same.

    In my example, what I am saying is: If I change a value in F19, call my macro, so the code will always fired when ever I change the value of F19. This could easily be a whole column or row. For e,g your could

    Target. Column =6
    When ever I change the value of any row in column F, my code will be triggered.

  6. #6
    Registered User
    Join Date
    07-20-2010
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    59

    Re: Worksheet Change Event

    Thanks AB,

    I tried the following but I get a Compile Error Message: Wrong number of arguments or invalid property assignment. Any ideas?

    Please Login or Register  to view this content.

  7. #7
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Worksheet Change Event

    Andrew,
    Before we do trial and error, could you please tell me which of your column/row is the triggering one? Which of your column/row you wish to change?

  8. #8
    Registered User
    Join Date
    07-20-2010
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    59

    Re: Worksheet Change Event

    AB,

    In the example above any change to row 16 should trigger the event. In reality I want it to be column 12 but the same thing happens if I change the above code from target.Row=16 to Target.Column = 12

    Andrew

  9. #9
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Worksheet Change Event

    Andrew,
    Okay, we should do the following:

    Please put this code in the sheet which have your data(Not module)

    Please Login or Register  to view this content.
    Please move the above code you have now to standard module. I used the name "Sub Andrewchange()"
    You can easily change this name to suite you need.
    In my case: If I change any value in column 12(On my sheet1) I will call my macro name "Andrewchange", which resides on my standard module.

  10. #10
    Registered User
    Join Date
    07-20-2010
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    59

    Re: Worksheet Change Event

    Hi Ab,

    OK I done this. Now the Worksheet Change Event does not seem to be triggered at all (but at least I don't get an error message!)

    Any more thoughts?

  11. #11
    Registered User
    Join Date
    07-20-2010
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    59

    Re: Worksheet Change Event

    It still works fine when run from the standard module.

  12. #12
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Worksheet Change Event

    Andrew,
    I assume you have 2 different codes:

    One is in your souce worksheet. ie right click on the sheet, copy and paste the above code.
    Second. rightly you have your regular code on the standard module
    Now change any value in column 12 to fire the code name "Andrewchange" and you should at least get an error message.

  13. #13
    Registered User
    Join Date
    07-20-2010
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    59

    Re: Worksheet Change Event

    Hi AB, Yes two codes one in the data sheet and one in a module. So that you can see better what is happening (or rather not happening) please see attached the excel file.
    Attached Files Attached Files

  14. #14
    Registered User
    Join Date
    07-20-2010
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    59

    Re: Worksheet Change Event

    AB,

    Sorry, that may have been a bad idea as the sheet is a copy from another workbook and the code references ranges in other sheets from that workbook.

  15. #15
    Registered User
    Join Date
    07-20-2010
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    59

    Re: Worksheet Change Event

    Here is the full worksheet. The change event is in sheet mFinanceAssumtions. The sub RepayCalc is in Module 1
    Attached Files Attached Files

  16. #16
    Registered User
    Join Date
    07-20-2010
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    59

    Re: Worksheet Change Event

    Oddly, it's now working! Thanks very much for all of your help.

    Cheers,

    Andrew

  17. #17
    Registered User
    Join Date
    07-20-2010
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    59

    Re: Worksheet Change Event

    Sorry Ab to bother you. Something rather strange happened. When I uploaded the file to this thread I changed its name. Then when I opened the file from this thread the code all worked as it should. I then changed the file name back to the original name and now the code doesn't do anything again. Any ideas on this?

  18. #18
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Worksheet Change Event

    Andrew,
    I just got back to my PC. As per the attached the code works. I have changed one value in column L and seems to work, so what is the issue?

  19. #19
    Registered User
    Join Date
    07-20-2010
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    59

    Re: Worksheet Change Event

    The issue is a very strange one. The code works when I upload the file from this thread. But when I make any changes to the file (e.g simply add a formula to another sheet in the file) the change event stops being triggered.

  20. #20
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Worksheet Change Event

    Ya, change event is not fire proof code, it sometimes does not behave like a normal code. Mind you, the change event can ONLY be triggered by change in column L, not by any other means, like formulas.

  21. #21
    Registered User
    Join Date
    07-20-2010
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    59

    Re: Worksheet Change Event

    I have been working with this file some more. It seems to suddenly stop working as I work with the file. However, if I save and close the file and then re-open it the code works again usually. Even better if I shut down the computer and restart.

  22. #22
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Worksheet Change Event

    Andrew,
    The main point is you have got somewhere to start with;this is your first event code and as you keep playing around with it, you would perfect it in no time.

  23. #23
    Registered User
    Join Date
    07-20-2010
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    59

    Re: Worksheet Change Event

    I know Ab and your help has been very much appreciated.

  24. #24
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Worksheet Change Event

    Hi AndrewMac

    Assuming you're NOT running on a Mac try this for your Change Event Code
    Please Login or Register  to view this content.
    Events were getting turned off and not back on...accordingly, the Event Code would not fire.
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  25. #25
    Registered User
    Join Date
    07-20-2010
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    59

    Re: Worksheet Change Event

    Jaslak,

    Thanks very much I have amended the code and it now seems to be working fine.

    Andrew (Not a Mac user)

  26. #26
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Worksheet Change Event

    Hi Andrew (not on a Mac)

    You're welcome...glad I could help.

    If this resolves your issue please mark your Thread as SOLVED.

    New quick method:
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

    Or you can use this way:
    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save

+ 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