+ Reply to Thread
Results 1 to 8 of 8

Getting runtime error when sub executed through userform change rather than directly

  1. #1
    Registered User
    Join Date
    03-07-2011
    Location
    Detroit, MI
    MS-Off Ver
    Excel 2003, 2007, & 2010
    Posts
    52

    Getting runtime error when sub executed through userform change rather than directly

    So I developed some code that hides/unhides certain rows based on selections on a userform. I want it to run when a change is made to one of the userform fields that affects what should be visible. The code seems to work great when I execute the macro directly however when I set the userform to call the macro when a change is made to the userform I get an error on the line where it defines the row the selected part number is listed ("PartNumberRow"). The specific error is "Run-time error '1004': Application-defined or object-defined error". What the heck am I missing here. Why would it run just fine when executed directely but not when it is called by a userform change. Thanks for all the help.

    Please Login or Register  to view this content.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Getting runtime error when sub executed through userform change rather than directly

    The part number might not have been found.

    This won't solve the problem but it will tell you if the find has failed
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    03-07-2011
    Location
    Detroit, MI
    MS-Off Ver
    Excel 2003, 2007, & 2010
    Posts
    52

    Re: Getting runtime error when sub executed through userform change rather than directly

    That shouldn't be possible. The only selectable options are actually pulled from the same 'Test Range'. I do know when I hover over the line in debug mode it is returning a 0 value. Still doesn't make sense why it would find it when executed directly but not when called by a userform change.

    I just tried adding your code to trigger the msg box and now "Set rngFnd = Range("Test_Range").Find(What:=PartNumber)" pulls the same error.

  4. #4
    Registered User
    Join Date
    03-07-2011
    Location
    Detroit, MI
    MS-Off Ver
    Excel 2003, 2007, & 2010
    Posts
    52

    Re: Getting runtime error when sub executed through userform change rather than directly

    When hovering over 'rngFnd' it shows "rngFnd = Nothing" It seems to be having a problem performing the find operation for whatever reason.

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Getting runtime error when sub executed through userform change rather than directly

    If you still get the error then the problem could be with the named range 'Test_Range'.

    Try adding a worksheet reference to it.
    Please Login or Register  to view this content.
    By the way, are the part numbers on the worksheet text? They would need to be to match the string PartNumber.

  6. #6
    Registered User
    Join Date
    03-07-2011
    Location
    Detroit, MI
    MS-Off Ver
    Excel 2003, 2007, & 2010
    Posts
    52

    Re: Getting runtime error when sub executed through userform change rather than directly

    I had tried defining the range further by defining the worksheet but it doesn't appear to make a difference. I still get the error.

    The "PartNumber" is a text string not a number. This goes for both the worksheet it is searching as well as the selection.

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Getting runtime error when sub executed through userform change rather than directly

    Can you upload an example workbook?

    Click on GO ADVANCED and use the paperclip icon to open the upload window.

    View Pic

    PS If you are going to cross post then post a link to the thread in the other forum.

  8. #8
    Registered User
    Join Date
    03-07-2011
    Location
    Detroit, MI
    MS-Off Ver
    Excel 2003, 2007, & 2010
    Posts
    52

    Re: Getting runtime error when sub executed through userform change rather than directly

    Okay so I still plan on uploading the file for review but I did find a work around.

    I changed the trigger of the macro from a change to the userform objects to a change to a "reference" sheet which contains control sources linked to the userform objects. In other words when I change the object in the userform it changes the linked control source cell in the "reference" sheet which then triggers the macro. Still no idea why I can't have the userform object change the trigger the macro without getting an error.

    I also found that it is not just an error with that line of code in particular. It appears to have a problem with any range reference, even if I define the workbook and sheet or try to put in code to activate the sheet prior to referencing a range in the sheet.

    I should have the file uploaded shortly. I just need to make sure there isn't any sensitive information attached to it. Again thanks for all the help.

+ 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