+ Reply to Thread
Results 1 to 13 of 13

Code Gives error if I run it from Module but works fine when run it from This Worksheet

  1. #1
    Forum Contributor
    Join Date
    11-11-2012
    Location
    Muscat, Oman
    MS-Off Ver
    Office 365
    Posts
    521

    Code Gives error if I run it from Module but works fine when run it from This Worksheet

    Hello Experts,
    I have a code below to delete few unwanted sheets and keep the sheets where I have SMRY as a part of the sheet name. This code works fine if I put it in This Worksheet. I get runtime error if I put it in module and try to operate the code from there. Tried changing the name to Sub XXX still got the error.
    From module it gets stuck at the line below
    Please Login or Register  to view this content.
    and the full code is
    Please Login or Register  to view this content.
    Please advise. Best Regards/VKS
    Last edited by VKS; 05-17-2013 at 02:12 PM.

  2. #2
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Code Gives error if I run it from Module but works fine when run it from This Workshee

    Syntactically the code is correct, and about the only error that it can raise is

    Method 'Delete' of object '_Worksheet' failed

    Which means it's trying to delete the last sheet. A workbook must have a least 1 worksheet...

  3. #3
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,770

    Re: Code Gives error if I run it from Module but works fine when run it from This Workshee

    Code seems to be perfect no errors found at my end


    If your problem is solved, then please mark the thread as SOLVED>>Above your first post>>Thread Tools>>
    Mark your thread as Solved


    If the suggestion helps you, then Click *below to Add Reputation

  4. #4
    Forum Contributor
    Join Date
    11-11-2012
    Location
    Muscat, Oman
    MS-Off Ver
    Office 365
    Posts
    521

    Re: Code Gives error if I run it from Module but works fine when run it from This Workshee

    Thanks for your time & Help Cytop & Sixthsense,
    The workbook has about 275 tabs out of which only 50 to 60 are SMRY (summary sheets) so there is no way that there is no worksheet (Not even hidden).
    Though i have my way around (paste the code in this workbook only) but it makes me think coz a devil sends me such a file every month.
    Best Regards/VKS
    On second thoughts do u think merged cells and hidden columns can effect the performance (but then they are there when you paste the code in this workbook also)..

  5. #5
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,770

    Re: Code Gives error if I run it from Module but works fine when run it from This Workshee

    If possible close that workbook and run the code on a new workbook by keeping it in a module and confirm

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

    Re: Code Gives error if I run it from Module but works fine when run it from This Workshee

    When you get the error what does this return when you try it in the Immediate Window(CTRL+G)?
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  7. #7
    Forum Contributor
    Join Date
    11-11-2012
    Location
    Muscat, Oman
    MS-Off Ver
    Office 365
    Posts
    521

    Re: Code Gives error if I run it from Module but works fine when run it from This Workshee

    Quote Originally Posted by Norie View Post
    When you get the error what does this return when you try it in the Immediate Window(CTRL+G)?
    Please Login or Register  to view this content.
    Surprisingly 1 but count is 275

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

    Re: Code Gives error if I run it from Module but works fine when run it from This Workshee

    That means, as cytop suggested, that the code is trying to delete all the worksheets.

    I think you might need to double check the sheet names.

  9. #9
    Forum Contributor
    Join Date
    11-11-2012
    Location
    Muscat, Oman
    MS-Off Ver
    Office 365
    Posts
    521

    Re: Code Gives error if I run it from Module but works fine when run it from This Workshee

    Quote Originally Posted by Norie View Post
    That means, as cytop suggested, that the code is trying to delete all the worksheets.

    I think you might need to double check the sheet names.
    I open the worksheet again ensuring only one workbook is open
    Then I go to immediate window and do
    ?thisworkbook.Worksheets.Count
    275
    Then I open the module run the code and get runtime error 1004
    I go back to immediate window and do
    ?thisworkbook.Worksheets.Count
    1 again.
    Something is surely wrong in running the code coz immediate window before running the code shows 275 and after running the code its same immediate window shows 1
    After getting 1 I go to first sheet called summary sheet right click and open view code then go to vba project and select Thisworkbook at the bottom and then paste the code hit F5 and do a control G and type ?thisworkbook.Worksheets.Count
    50 is what I get
    I agree that code is wrong but I don’t think its deleting all the sheets.

  10. #10
    Forum Contributor
    Join Date
    11-11-2012
    Location
    Muscat, Oman
    MS-Off Ver
    Office 365
    Posts
    521

    Re: Code Gives error if I run it from Module but works fine when run it from This Workshee

    Dear Friends,
    John H Davis on this forum gave me a the code below which ran very well from the module when i opend the file first hence sharing (Will try it cpl of times more) before marking this thread as solved. Thanks John.
    Please Login or Register  to view this content.

  11. #11
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,770

    Re: Code Gives error if I run it from Module but works fine when run it from This Workshee

    Little bit of changes in your post #10 code

    Please Login or Register  to view this content.

  12. #12
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    Are you sure the code is running on the correct workbook?

    ThisWorkbook refers to the workbook the code is in.

  13. #13
    Forum Contributor
    Join Date
    11-11-2012
    Location
    Muscat, Oman
    MS-Off Ver
    Office 365
    Posts
    521

    Re: Code Gives error if I run it from Module but works fine when run it from This Workshee

    @ Norie: I ensured other work books were closed.
    @ Norie, Sixthsense & cytop : Thanks for your time and help you all are stars and have my stars
    Since I have my way around and a new code i think i should mark this thread as solved
    Thanks once again

+ 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