+ Reply to Thread
Results 1 to 7 of 7

Sub won't work on change event

  1. #1
    Valued Forum Contributor
    Join Date
    03-23-2008
    Location
    Tennessee
    MS-Off Ver
    Office 2007
    Posts
    706

    Sub won't work on change event

    Greetings, Gurus.

    I have a small macro that filters and copied data to a "Main" sheet. It works beautifully if I run it from a standard module, but when I try to run it from a Worksheet Change event, it always crashes Excel. Here is the code:

    Please Login or Register  to view this content.
    Now, I've never run a macro from a worksheet change event, but I read some examples on here, so I right clicked on the "Main" worksheet, and copied my code between these lines:
    Please Login or Register  to view this content.
    Of course, I removed "Sub Get_Data()" from the top, and "End Sub" from the bottom, but as I said, Excel keeps crashing. What am I doing wrong?

    Thanks in advance for any help you can offer.
    Last edited by hutch@edge.net; 03-10-2010 at 02:11 PM.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Sub won't work on change event

    I don't know how you've actually coded this, but you need to disable events (application.enablevents=false) before doing anything (like pasting data) that will trigger the change event again, and then re-enable events when you're done.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Valued Forum Contributor
    Join Date
    03-23-2008
    Location
    Tennessee
    MS-Off Ver
    Office 2007
    Posts
    706

    Re: Sub won't work on change event

    How's it going, Shg? I'm getting the error "Object Method of Range Class Failed"

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Sub won't work on change event

    Post the code your using, and say which line falls over.

  5. #5
    Valued Forum Contributor
    Join Date
    03-23-2008
    Location
    Tennessee
    MS-Off Ver
    Office 2007
    Posts
    706

    Re: Sub won't work on change event

    I apologize, shg. I was in a hurry yesterday afternoon.

    I have pulled all the sensitive data out of the workbook, and attached a sample to this message. I thought this approach might be easier for both of us.

    The macro works beautifully if you enter the 3 digit location number, and then click the "Get Data" button. My goal is to have the macro run anytime the user changes the location number in D4, (now on version 3 ). I don't currently have the macro stored anywhere except a standard module.

    I have all pages locked, with no password, and all but the main page are hidden. The macro unlocks them and unhides them to copy the data, but then locks and hides them again before ending.

    Thanks again for all your help!
    Attached Files Attached Files

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Sub won't work on change event

    Hutch,

    In the Main sheet module:
    Please Login or Register  to view this content.
    In the code module:
    Please Login or Register  to view this content.
    All completely untested ...

  7. #7
    Valued Forum Contributor
    Join Date
    03-23-2008
    Location
    Tennessee
    MS-Off Ver
    Office 2007
    Posts
    706

    Re: Sub won't work on change event

    I was getting the error message box on all instances, but I replaced the module code with my woking code, and it works great now, (although not as neat as yours). I think my problem was that I was putting the whole code in the worksheet change module, rather than calling it from a standard module, and I didn't have the IF argument in the worksheet module.

    Thanks again, shg. This is another one SOLVED!

    Have a good one.

+ 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