Delete specific sheets based on sheet name

     

  • Delete sheets, having sheet name as PURCHASE
  •  

    Private Sub CommandButton1_Click()
    Application.DisplayAlerts = False
    Dim max As Integer
    max = Sheets.Count
    For i = 1 To max
    If InStr(1, Sheets(i).Name, "purchase") Then
    Sheets(i).Delete
    End If
    Next
    Application.DisplayAlerts = True
    End Sub

     

     

    Delete all sheets except specific sheets

     

    Private Sub CommandButton1_Click()
    Application.DisplayAlerts = False
    Dim sh As Worksheet
    For Each sh In ThisWorkbook.Sheets
    If sh.Name <> "sheet2" And sh.Name <> "sheet1" Then
    sh.Delete
    End If
    Next
    Application.DisplayAlerts = True
    End Sub

     

     

    Delete blank sheets - For loop

     

    Private Sub CommandButton1_Click()
    Application.DisplayAlerts = False
    Dim shcount As Integer
    shcount = Sheets.Count
    For i = 1 To shcount
    If Application.WorksheetFunction.CountA(Sheets(i).Cells) = 0 Then
    Sheets(i).Delete
    End If
    Next
    Application.DisplayAlerts = True
    End Sub

     

    Delete blank sheets - For Each loop

     

    Private Sub CommandButton1_Click()
    Application.DisplayAlerts = False
    Dim sh As Worksheet
    For Each sh In ThisWorkbook.Sheets
    If Application.WorksheetFunction.CountA(sh.Cells) = 0 Then
    sh.Delete
    End If
    Application.DisplayAlerts = True
    Next
    End Sub

     

     

    Delete specific sheet - For loop

     

    Private Sub CommandButton1_Click()
    Application.DisplayAlerts = False
    Dim shcount As Integer
    shcount = Sheets.Count
    Dim i As Integer
    For i = 1 To shcount
    If Sheets(i).Name = "Sheet4" Then
    Sheets(i).Delete
    End If
    Application.DisplayAlerts = True
    End Sub

     

     

    Delete specific sheet - For each loop

     

    Private Sub CommandButton1_Click()
    Application.DisplayAlerts = True
    Dim sh As Worksheet
    For Each sh In ThisWorkbook.Sheets
    If sh.Name = "Sheet4" Then
    sh.Delete
    End If
    Next
    Application.DisplayAlerts = False
    End Sub

     

     

    Delete sheets - Based on TAB color

     

    Add sheets and color it
    Private Sub CommandButton1_Click()
    Dim i As Integer
    i = 11
    For i = 1 To 11
    If i > Sheets.Count Then
    Sheets.Add after:=Worksheets(Sheets.Count)
    End If
    If i Mod 2 = 0 Then
    Sheets(i).Tab.ColorIndex = 5
    Else
    Sheets(i).Tab.ColorIndex = 7
    End If
    Next
    End Sub
    delete sheets based on color
    Private Sub CommandButton2_Click()
    Application.DisplayAlerts = False
    Dim shcnt As Integer
    shcnt = Sheets.Count
    For i = 1 To shcnt
    If Sheets(i).Tab.ColorIndex = 7 Then
    Sheets(i).Delete
    End If
    Next
    Application.DisplayAlerts = True
    End Sub

    Movement of sheets from one WKB to another

     

     

     

  • Sheets movement from one workbook to another
  •  

    Private Sub CommandButton1_Click()
    Dim cnt As Integer
    cnt = Sheets.Count
    Dim wkb As Workbook
    Set wkb = Workbooks.Add
    For i = 3 To cnt
    ThisWorkbook.Sheets(i).Move after:=wkb.Sheets(wkb.Sheets.Count)
    ActiveSheet.Name = ThisWorkbook.Sheets(i).Name & " moved"
    Next
    End Sub

     

     

     

     

    Movement of sheets in alphabhet order

     

    Private Sub CommandButton1_Click()
    For Max = 1 To Sheets.Count
    For j = 1 To Sheets.Count - 1
    If UCase$(Sheets(j).Name) > UCase$(Sheets(j + 1).Name) Then
    Sheets(j).Move after:=Sheets(j + 1)
    End If
    Next
    Next
    End Sub

     

     

    Movement of sheets in after particular sheet

     

    Private Sub CommandButton1_Click()
    Dim a
    Dim i As Integer
    ReDim a(1 To Sheets.Count)
    For i = 1 To Sheets.Count
    a(i) = Sheets(i).Name
    Next
    For i = UBound(a) To 1 Step -1
    Sheets(a(i)).Move after:=Worksheets(8)
    Next
    End Sub

    VBA Sheet Movement

     

  • Through this key word we can create new work sheet and place as per our requirement
  • We can move active sheet as per our requirement
  • we can move the sheet before\after a particular sheet
  •  

    How to position newly creaed worksheet

     

     

    • Before:New sheet should place before which sheet
    •  

    • After:New sheet should place After which sheet
    •  

    • Count:Number of sheets to add

     

    Write a program to add new sheet at, subsequent to last sheet:

     

     

    Write a program to add new sheet before a particular sheet:

     

     

    Write a program to create 11 sheets before sheet of GURU

     

     

     

    Write a program to create 11 sheets After sheet of GURU:

     

     

     

    Write a program to create new sheet after last sheet

     

     

     

    Write a program to move 2nd sheet to last sheet