+ Reply to Thread
Results 1 to 7 of 7

Deleting Sheets Macro

  1. #1
    Registered User
    Join Date
    02-05-2008
    Posts
    17

    Deleting Sheets Macro

    Hi Everyone,

    I wrote the function below so it could
    1) Clear all contents in sheet 1 range C9:AI50,AK9:AN50
    2) Rename worksheet 2 to "TEMPLATE" and cell A1 in worksheet 2 to "TEMPLATE"
    3) Delete all other worksheets in the workbook but sheet 1 and 2.

    Now Here is my issue, this code works, expect for when I have only 2 worksheets?? Can someone give me some insight on what I may be doing wrong??

    Thanks!


    Please Login or Register  to view this content.

  2. #2
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988
    Add a if statement

    e,g

    Please Login or Register  to view this content.
    VBA Noob
    _________________________________________


    Credo Elvem ipsum etian vivere
    _________________________________________
    A message for cross posters

    Please remember to wrap code.

    Forum Rules

    Please add to your signature if you found this link helpful. Excel links !!!

  3. #3
    Registered User
    Join Date
    02-05-2008
    Posts
    17
    Hi VBA Noob,

    I tried that but doesn't seem to work...

    If I add that If Statement then it works properly when their are two sheets, but not when there are more than two...

    I am not sure why?

    Thanks again!

  4. #4
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988
    Try

    Please Login or Register  to view this content.
    VBA Noob

  5. #5
    Forum Expert dominicb's Avatar
    Join Date
    01-25-2005
    Location
    Lancashire, England
    MS-Off Ver
    MS Office 2000, 2003, 2007 & 2016 365
    Posts
    4,867

    Smile

    Good evening Excel Newbie05

    It seems to be because of the line :
    Please Login or Register  to view this content.
    If Worksheets.Count is less than three, then it cannot count backwards unless you qualify the statement with a Step -1 instruction. The loop will do nothing.

    As you variables (j in particular) are quantified within the loop, they are set to 0 outside the loop, Excel is not picking up and deleting the sheets it should.

    HTH

    DominicB
    Please familiarise yourself with the rules before posting. You can find them here.

  6. #6
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988
    To turn off the sheet delete message add this at the start

    Please Login or Register  to view this content.
    and this at the end
    Please Login or Register  to view this content.
    VBA Noob

  7. #7
    Registered User
    Join Date
    02-05-2008
    Posts
    17
    Thank you VBA Noob! This worked Wonders!!

+ 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