+ Reply to Thread
Results 1 to 5 of 5

Code works in change event but not in module?

  1. #1
    Forum Contributor
    Join Date
    09-16-2013
    Location
    Los Angeles, USA
    MS-Off Ver
    Excel 2011
    Posts
    620

    Code works in change event but not in module?

    Im having problems with the following code. Im planning on using it as part of some userform command button code, but Im separating it out until I can get it working.
    I am using the exact same code in my Worksheet Change event code without any issue.
    The code should find records from A6 on (to last row in G), and if data exists in G but not in D on the same row, then it should fill it with todays date. If data exists in D but not in G on the same row then D should be cleared.
    The code is giving me run time error '424' (Object Required) on this line If Intersect(Target, Range("A6:K"......

    Please Login or Register  to view this content.
    Thanks,
    James

  2. #2
    Forum Contributor
    Join Date
    09-16-2013
    Location
    Los Angeles, USA
    MS-Off Ver
    Excel 2011
    Posts
    620

    Re: Code works in change event but not in module?

    I dont know why it didn't work, but I figured out another way to code it, to effectively give me the same thing:
    Please Login or Register  to view this content.

  3. #3
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,613

    Re: Code works in change event but not in module?

    When you use the With ... End With construct, you need to use a full stop /decimal point / dot to link the items to the objects. So you need a dot in front of each and every reference to Range or Cells.

    The second version works more by good luck than anything and might fail or give unexpected results if the ReturnData worksheet is not selected (the active sheet).

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  4. #4
    Forum Contributor
    Join Date
    09-16-2013
    Location
    Los Angeles, USA
    MS-Off Ver
    Excel 2011
    Posts
    620

    Re: Code works in change event but not in module?

    Thanks TMS, I though since the code specifies With Sheets ("ReturnData") that forces it to work with that sheet only?
    Also the userform its working on is only generated by that sheet event procedure.

  5. #5
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,613

    Re: Code works in change event but not in module?

    Like I said, luck. Read up on With ... End With.

    Oh, and the first code fails because the Target range only exists in the Change event handler. When you take the code out of context it has no range object.

    Regards, TMS

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Textbox Change event handler works once
    By Solus Rankin in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 05-30-2013, 12:28 PM
  2. [SOLVED] Code Gives error if I run it from Module but works fine when run it from This Worksheet
    By VKS in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 05-17-2013, 01:18 PM
  3. change the (Worksheet_Change) event for a module (Sub).
    By marreco in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-07-2012, 01:37 PM
  4. Run Event Code In General Module
    By Who I Am in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-26-2006, 02:30 PM
  5. [SOLVED] Where?Worksheet code module or Worksheet_SelectionChange event han
    By Kenzie in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 01-30-2006, 05:45 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