none
HOW TO DELETE SHEETS RRS feed

  • Question

  • Hi,

    how to delete sheets if they are present in workbook. ordinary code give error if sheet is not present in workbook.

    say if sheets apple (1), apple (2), apple (3), apple (4) and so on. how to delete all apple () sheets if present and no error if not present.

    regards

    Sunday, November 29, 2015 4:20 PM

Answers

  • Try this then:

        Dim I As Long
        Application.DisplayAlerts = False
        For I = Worksheets.Count To 1 Step -1
            If Worksheets(I).Name Like "apple (*)" Then
                Worksheets(I).Delete
            End If
        Next I
        Application.DisplayAlerts = True


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Sunday, November 29, 2015 7:43 PM

All replies

  • For example:

        Dim I As Long    Application.DisplayAlerts = False
        For I = Worksheets.Count To 1 Step -1
            If Worksheets(I).Name Like "apple*" Then
                Worksheets(I).Delete
            End If
        Next I
        Application.DisplayAlerts = True


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Sunday, November 29, 2015 5:20 PM
  • Hi ,

    Thank you Hans Vogelaar ,

    I do not want to delete sheet with name Apple, only Apple (1), Apple (2) etc.

    or

    if sheets("Apple (1)").visible=true then sheet.delete  how to make it work for all Apple () sheet.

    when I use this way it delete only one sheet in one event . Delete another in next event .

    regards. 

    Sunday, November 29, 2015 5:34 PM
  • Try this then:

        Dim I As Long
        Application.DisplayAlerts = False
        For I = Worksheets.Count To 1 Step -1
            If Worksheets(I).Name Like "apple (*)" Then
                Worksheets(I).Delete
            End If
        Next I
        Application.DisplayAlerts = True


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Sunday, November 29, 2015 7:43 PM
  • Thank you Hans Vogelaar ,

    For your kindness .

    regards.

    Monday, November 30, 2015 1:10 AM