+ Reply to Thread
Results 1 to 9 of 9

Vba Loop not working

  1. #1
    Registered User
    Join Date
    01-31-2014
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    15

    Vba Loop not working

    Hi Guys,
    I need urgent help with respect to the above

    I need my code to look through values(whose values are coincidentally the same as some worksheet names) on a worksheet called "Summary" and then delete any visible worksheet that does not have its name equal to the values on the "Summary" worksheet. have got everything set up but it appears that my loop does not work accurately as it still does not delete some worksheets whose names are not values on the Summary tab.

    Please Login or Register  to view this content.

    Please help me out!!!

  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: Vba Loop not working

    Where do you refer to the sheets by name?
    If posting code please use code tags, see here.

  3. #3
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,623

    Re: Vba Loop not working

    the problem could be here because you refer to worksheets collection by item number, and in a meanwhile you delete some sheets changing collection items numbering:
    Please Login or Register  to view this content.
    try:
    Please Login or Register  to view this content.
    and have a look on the result after the code ended



    Based on the above: just an idea - may be deleting bottom-up will help in your situation:

    Please Login or Register  to view this content.
    Best Regards,

    Kaper

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

    Re: Vba Loop not working

    Try this, it will loop through all worksheets and see if there name appears on the list in column A on the 'Summary' sheet.

    If a worksheet's name doesn't appear on the list it's deleted.
    Please Login or Register  to view this content.
    Last edited by Norie; 02-27-2014 at 12:36 PM.

  5. #5
    Registered User
    Join Date
    01-31-2014
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Vba Loop not working

    Hi Guys, thanks for your help.

    Kaper... the code you gave above is not working. It just copies the names of the worksheets(even the hidden ones) and pastes them in A2 of the Summary sheet and moves downwards. This is not what I want the code to do sadly.

    Norie,... your code just deletes all the worksheets!!

    I need help guys!!

  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: Vba Loop not working

    Can you upload an example workbook?

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

  7. #7
    Registered User
    Join Date
    01-31-2014
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Vba Loop not working

    Hi Norie,

    Just had another look at your code and did some tweaking and it now works. What I did was...
    Please Login or Register  to view this content.
    Thanks a lot Norie and Kaper(for taking time out to post a solution). I really appreciate it

    This forum ROCKS!!!!!!!!!!!!!!

  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: Vba Loop not working

    I assumed Summary would have been on the list.

  9. #9
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,623

    Re: Vba Loop not working

    Quote Originally Posted by dahveedoff View Post
    ...It just copies the names of the worksheets(even the hidden ones) and pastes them in A2 of the Summary sheet and moves downwards.
    When I wrote: "have a look on the result after the code ended" - I ment that you shall compare "by eye" what your code was to compare here:

    Please Login or Register  to view this content.
    to let you made ammendments after you know what code really compares.

    The code you posted now
    Please Login or Register  to view this content.
    does something else than described in the first post:

    delete any visible worksheet that does not have its name equal to the values on the "Summary" worksheet.
    it deletes all visible sheets except Summary, not looking at all into the content of Summary Sheet.

    Anyway, I'm glad, it works as you want, and it's probably time to mark thread as SOLVED (if you need instructions how to do it - see forum rules)

+ 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] For Next Loop not working
    By jacob@thepenpoint in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-12-2013, 02:24 PM
  2. Loop not working...
    By yunesm in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-10-2011, 08:12 AM
  3. Loop not working
    By blankus in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-14-2009, 04:16 AM
  4. Loop not working!!
    By Simon in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-02-2005, 12:05 PM
  5. [SOLVED] Do...Loop not working
    By Sunny Lin in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-13-2005, 09:06 PM

Tags for this Thread

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