+ Reply to Thread
Results 1 to 8 of 8

Run time error 40036, similar procedures - works in one sheet not in the other

  1. #1
    Registered User
    Join Date
    01-12-2012
    Location
    Rockland, WI
    MS-Off Ver
    Excel 2016
    Posts
    56

    Run time error 40036, similar procedures - works in one sheet not in the other

    I have 4 procedures in a workbook to clear worksheets’ rows; 3 of the procedures work, one generates “Run-time error 40036, Application defined or Object defined error”. My only difference in the procedures is the worksheet name. Can someone tell me why procedure “Public Sub ClrComparison()” generates the error? They are in module “modClearSheetRows”.

    These three work:
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    This one generates the run time error

    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by jdfjab; 06-14-2012 at 09:24 AM.

  2. #2
    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,935

    Re: Run time error 40036, similar procedures - works in one sheet not in the other

    The only obvious difference is that the comparison sheet has blank lines in it.

    Try deleting the blank lines, for the purpose of the test, and running your macro again. If that works, you need another way of generating the range to be cleared. I'm not a fan of UsedRange as it can be unreliable.

    I tend to use Range("A" & Rows.Count).End(xlUp).Row to get the genuine last row
    and Cells(Columns.Count, 1).End(xlToLeft).Column to get the last column.

    You can then use these to define the range to be cleared.


    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


  3. #3
    Forum Contributor
    Join Date
    04-11-2011
    Location
    Columbus, Ohio
    MS-Off Ver
    Excel 2007
    Posts
    325

    Re: Run time error 40036, similar procedures - works in one sheet not in the other

    jdfjab,

    I copied the data from "Comparison" to a new worksheet, deleted "Comparison, and renamed the new sheet "Comparison". The code proceeded to work without errors. I do not know why you got the error in the first place. Maybe excel glitched and couldn't correctly find the used range. Anyways, I would take TMShucks advice on finding ranges. It is more reliable.

    Best of luck,

    111StepsAhead

  4. #4
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: Run time error 40036, similar procedures - works in one sheet not in the other

    Hi, I'm not sure why, but the reason for the error is that you have declared your own type sKey in the sheet module for sheet Comparison.
    If you try to compile your project you will get error that you cannot define Public user-defined type in object module. if you move this to normal module or just comment it, it will work just fine.
    Buran
    If you are pleased with a member's answer then use the Star icon to rate it.

  5. #5
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: Run time error 40036, similar procedures - works in one sheet not in the other

    Quote Originally Posted by buran View Post
    Hi, I'm not sure why, but the reason for the error is that you have declared your own type sKey in the sheet module for sheet Comparison.
    If you try to compile your project you will get error that you cannot define Public user-defined type in object module. if you move this to normal module or just comment it, it will work just fine.
    Buran
    Note that I refer to following code

    Please Login or Register  to view this content.
    that is right after Option Explicit statement

  6. #6
    Registered User
    Join Date
    01-12-2012
    Location
    Rockland, WI
    MS-Off Ver
    Excel 2016
    Posts
    56

    Re: Run time error 40036, similar procedures - works in one sheet not in the other

    Thanks Buran,
    Originally I was trying to build an array to sort my differences between periods, but got stumped and resorted to putting the values into a new worksheet and sorting.
    Can you clue me in to how you saw that? I'm new to VBA and have trouble deciphering the cause of error messages and the associated "help" available when an error is generated.

    Truly appreciate the help.
    Regards,
    JDF

  7. #7
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: Run time error 40036, similar procedures - works in one sheet not in the other

    I was not able to see anything wrong with your code, so I decided to try really simple thing - I activated the Comparison sheet and tried to execute Debug.Print ActiveSheet.Name in the Immediate Window and I get the same error. Now, knowing that there is something wrong with the worksheet, not your code, I tried to compile the project and get the error mentioned in my reply. So, I commented out the problem code and it worked just fine. It makes sense, because your code was not compiled and it compiles the project on the fly. so it rise an error only when it uses worksheet Comparison and tries to compile the code in the object module for that sheet.
    I hope I make myself clear and it helps on how I debug the error.
    Buran

  8. #8
    Registered User
    Join Date
    01-12-2012
    Location
    Rockland, WI
    MS-Off Ver
    Excel 2016
    Posts
    56

    Re: Run time error 40036, similar procedures - works in one sheet not in the other

    Thanks TMS, Buran, & 111StepsAhead.
    You all "rock".
    Dumb as I am, I always pick something up in the "awesome" help given.
    Regards,
    JDF

+ 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