+ Reply to Thread
Results 1 to 5 of 5

Private Sub for manual change to range of cells

  1. #1
    Registered User
    Join Date
    02-23-2011
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    5

    Question Private Sub for manual change to range of cells

    Hello!

    I am trying to create a macro that will run only when selected values in a sheet are manually modified. Currently, I have a private sub linked to the relevant worksheet, which detects change in the following fashion:



    Please Login or Register  to view this content.

    As you will have guessed, I have a problem because this macro runs whenever I manually enter a change but also when the VBA coding changes the target cells. And since the macro triggered by change to the target range also includes further changes to the target range, it runs continually and I need to stop it using Ctl + break.

    Could anyone help me either find a way to differentiate between manual and automatic (i.e. VBA) change, or provide me with a different macro that will run only once if a value in the target range is changed?

    Thank you very much for your time and help!

    Regards,

    Calypso
    Last edited by ogygiasylph; 02-23-2011 at 10:17 AM. Reason: The thread was solved so I changed the title to reflect this.

  2. #2
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    2003, 2007, 2010, 2013
    Posts
    1,252

    Re: Private Sub for manual change to range of cells

    Hi,

    Before you change the cell values in your code, you need to temporarily set Application.EnableEvents to False so that your Worksheet_Change event handler isn't called. You must ensure it is set back to True again once the changes have been made.
    Hope that helps,

    Colin

    RAD Excel Blog

  3. #3
    Registered User
    Join Date
    02-23-2011
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Private Sub for manual change to range of cells

    So would I have to disable events (using the formula you just gave) me immediately after

    Please Login or Register  to view this content.
    And enable them again at some point before the end of the macro?

    Thank you!

  4. #4
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    2003, 2007, 2010, 2013
    Posts
    1,252

    Re: Private Sub for manual change to range of cells

    Yes, exactly. But you need to ensure that you turn EnableEvents back on even if an error occurs, so I would suggest a structure such as this:
    Please Login or Register  to view this content.
    There's one exit point in this routine, and Application.EnableEvents = True is always called.

  5. #5
    Registered User
    Join Date
    02-23-2011
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Private Sub for manual change to range of cells

    Phenomenal. It works like a charm. Thank you very much!

+ 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