+ Reply to Thread
Results 1 to 7 of 7

Deleting Sheets Macro

Hybrid View

  1. #1
    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!

  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
    Try

    Sub RenameTemplateSheet()
    
    Dim numOfShts As Integer
        numOfShts = 0
    If Sheets.Count > 2 Then
        For i = 3 To Worksheets.Count
    
            If IsEmpty(Worksheets(i).Range("A6")) Then
                numOfShts = numOfShts + 1
            End If
        Next i
       
        Dim sheetsArray() As Variant
        ReDim sheetsArray(numOfShts - 1)
        j = 0
        
        For iSheet = 3 To Worksheets.Count
            If IsEmpty(Worksheets(iSheet).Range("A6")) Then
               sheetsArray(j) = Worksheets(iSheet).Name
               j = j + 1
            End If
        Next iSheet
    
        Worksheets(sheetsArray).Delete
        
        
    Worksheets(1).Range("C9:AI50,AK9:AN50").ClearContents
        
        
    Worksheets(2).Name = "TEMPLATE"
    Worksheets(2).Range("A1") = "TEMPLATE"
    
    Else
    MsgBox "There are only " & Sheets.Count & " sheets in workbook"
    End If
    
    
    End Sub
    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
    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 :
    For i = 3 To Worksheets.Count
    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.

  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
    To turn off the sheet delete message add this at the start

    Application.DisplayAlerts = False
    and this at the end
    Application.DisplayAlerts = True
    VBA Noob

  5. #5
    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