Closed Thread
Results 1 to 9 of 9

Using Worksheet_SelectionChange, so code only runs when specific cells are changed

  1. #1
    Registered User
    Join Date
    11-15-2009
    Location
    Palmer, Alaska
    MS-Off Ver
    Excel 2010
    Posts
    69

    Using Worksheet_SelectionChange, so code only runs when specific cells are changed

    Hello,

    I am trying to use the Worksheet_SelectionChange event for the first time instead of using the Worksheet_Change event in the hopes that it will only run the macros when specific cells are changed. This is because when it runs, there is a very long lag time, which holds up the ability to use the spreadsheet if the change event is used. So, since the macros are only needed rarely when specific cells are changed, I was hoping the selection change event would work.

    Below is part of the code I came up with. I need the code to run when there are changes to cells R4, P1, P2, A10, and A14. The code is actually much longer with many more like A10 and A14, but I omitted them for this example.

    I tried just using two of the targets in the example I found online, If Target.Address = "$R$4" Or Target.Address = "$P$1" Then MsgBox "Success".But it isn't working, so I obviously included it wrong.




    Please Login or Register  to view this content.
    Last edited by Kristine T.; 02-19-2015 at 11:32 PM.

  2. #2
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,168

    Re: Using Worksheet_SelectionChange, so code only runs when specific cells are changed

    Hi Kristine,

    The normal check for the Event Firing is like this:

    Please Login or Register  to view this content.
    Read more about it at:
    http://www.ozgrid.com/VBA/vba-intersect.htm

    Also could you edit your post above, select the code lines and click on the "#" sign to put Code tags around it. That makes it much more readable for us humans. I've done this with my line of code above.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  3. #3
    Registered User
    Join Date
    11-15-2009
    Location
    Palmer, Alaska
    MS-Off Ver
    Excel 2010
    Posts
    69

    Re: Using Worksheet_SelectionChange, so code only runs when specific cells are changed

    Thanks! Good to know about the # sign for the code. I need to indicate specific cells are targets. That seemed to exclude them and I'm not sure how to add it. I tried replacing the second line with it and I tried from the link you provided...

    Please Login or Register  to view this content.
    But they didn't work. I could just be adding it wrong, though.

  4. #4
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,168

    Re: Using Worksheet_SelectionChange, so code only runs when specific cells are changed

    Hey,

    Why again SelectionChange instead of simply Change??

    See this code and see it work, behind a worksheet.

    Please Login or Register  to view this content.
    Change any cell except for those listed in the Union above and code will only stop when you change one of them.

  5. #5
    Registered User
    Join Date
    11-15-2009
    Location
    Palmer, Alaska
    MS-Off Ver
    Excel 2010
    Posts
    69

    Re: Using Worksheet_SelectionChange, so code only runs when specific cells are changed

    Thank you again! However, I think there was a miscommunication, because I want the opposite to happen. Instead of, "and code will only STOP when you change one of them.", I need it to only RUN when I change one of them.

  6. #6
    Registered User
    Join Date
    11-15-2009
    Location
    Palmer, Alaska
    MS-Off Ver
    Excel 2010
    Posts
    69

    Re: Using Worksheet_SelectionChange, so code only runs when specific cells are changed

    I think I'm going to try the commercial services option, since this is the last thing before it is finished and ready for the Department to start using.

  7. #7
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,168

    Re: Using Worksheet_SelectionChange, so code only runs when specific cells are changed

    Hi Kristine,

    I really think my code above does what you are asking for. If you change any of these cells
    Range("R4"), Range("P1:P2"), Range("A10"), Range("A14")
    the code will run, otherwise it will skip down to the End If statement and not run.

    I'm sorry that I can't explain it better.. Perhaps if you were to attach a sample workbook and show us what you are trying to do?

  8. #8
    Registered User
    Join Date
    11-15-2009
    Location
    Palmer, Alaska
    MS-Off Ver
    Excel 2010
    Posts
    69

    Re: Using Worksheet_SelectionChange, so code only runs when specific cells are changed

    I just tried, but unfortunately my file size is twice the limit.

  9. #9
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Using Worksheet_SelectionChange, so code only runs when specific cells are changed

    Closed at request of OP so that question may be posted to Commercial Services.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

Closed Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. VBA that runs when cell within range is changed to specific value
    By KT99 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-24-2014, 12:07 PM
  2. Code to do Save as a specific cell value anytime the sheet is changed
    By superchew in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-23-2014, 12:14 PM
  3. notify, when specific cells are changed
    By ossa in forum Excel General
    Replies: 3
    Last Post: 11-14-2012, 09:00 AM
  4. Code runs only if a specific page of the MultiPage is selected
    By Gabor in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-18-2009, 07:18 PM
  5. [SOLVED] keep specific cells from being changed.
    By bronsonbits in forum Excel General
    Replies: 1
    Last Post: 09-07-2005, 03:05 PM

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