Code to Delete Sheet Tabs

Justinian

Well-known Member
Joined
Aug 9, 2009
Messages
1,557
Office Version
  1. 365
Platform
  1. Windows
I have a code I run to split data into tab named after managers. All managers are together on sheet1 and after I run the code, each manager's data is on its own tab. The problem is the when the code is run again, it adds data on each tab instead of replacing the data on each tab. How can I construct some code so Excel deletes all tabs to the right of my data sheet before running my original code to separate tabs?
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
I have other sheets to the left of the All Accounts tab that I also do not want cleared.
 
Upvote 0
Try running the code below as a standalone. It should clear all the sheets to the right of the "All Accounts" sheet.

Code:
Sub clearshts()
    Dim i As Long
    For i = Sheets("All Accounts").Index + 1 To Sheets.Count
        Sheets(i).Cells.ClearContents
    Next
End Sub
 
Upvote 0
I see now. Mark has the answer. I was still pondering that one.
 
Last edited:
Upvote 0
Try running the code below as a standalone. It should clear all the sheets to the right of the "All Accounts" sheet.

Code:
Sub clearshts()
    Dim i As Long
    For i = Sheets("All Accounts").Index + 1 To Sheets.Count
        Sheets(i).Cells.ClearContents
    Next
End Sub

Is there no way to make this part of the first code?
 
Upvote 0
This works! The only problem is that it clears the data but leaves formatting like borders and shading. How can I fix that?
 
Upvote 0
This works! The only problem is that it clears the data but leaves formatting like borders and shading. How can I fix that?

You would change
Code:
Sheets(i).Cells.ClearContents
to
Code:
Sheets(i).Cells.Delete

Is there no way to make this part of the first code?
Of course it can be part of another code, the reason for testing it separately is to make sure it works without anything else interfering with it before integrating it in the code.

Put
Code:
Dim irw As Long
with the rest of the lines starting with Dim.

then put the lines below at the point that you want the sheets cleared (you can use F8 to step through your code line by line if you need to see what your code does at each line).

Code:
    For irw = Sheets("All Accounts").Index + 1 To Sheets.Count
        Sheets(irw).Cells.Delete
    Next

Please note that I only changed the variable from i to irw as you are already using, i just to make it less confusing.
 
Last edited:
Upvote 0
Make sure that the "All Accounts" sheet is the active sheet and try this macro:
Code:
Sub AddSheet()
    Application.ScreenUpdating = False
    Dim bottomG As Long
    bottomG = Range("G" & Rows.Count).End(xlUp).Row
    Dim c As Range
    Dim ws As Worksheet
    If Sheets("All Accounts").FilterMode Then Sheets("All Accounts").ShowAllData
    For Each c In Range("G2:G" & bottomG)
        If c <> "" Then
            Set ws = Nothing
            On Error Resume Next
            Set ws = Worksheets(c.Value)
            On Error GoTo 0
            If ws Is Nothing Then
                Worksheets.Add(After:=Sheets(Sheets.Count)).Name = c.Value
                Sheets("All Accounts").Range("A1:N" & bottomG).AutoFilter Field:=7, Criteria1:=c
                Sheets("All Accounts").Range("A1:N" & bottomG).SpecialCells(xlCellTypeVisible).EntireRow.Copy ActiveSheet.Cells(1, 1)
                ActiveSheet.Columns.AutoFit
            Else
                Sheets(c.Value).UsedRange.ClearContents
                Sheets("All Accounts").Range("A1:N" & bottomG).AutoFilter Field:=7, Criteria1:=c
                Sheets("All Accounts").Range("A1:N" & bottomG).SpecialCells(xlCellTypeVisible).EntireRow.Copy Sheets(c.Value).Cells(1, 1)
                Sheets(c.Value).Columns.AutoFit
            End If
            If Sheets("All Accounts").FilterMode Then Sheets("All Accounts").ShowAllData
        End If
    Next c
    Application.ScreenUpdating = True
 End Sub
 
Upvote 0
Try this:
Code:
Sub clearshts()
    Dim i As Long
    For i = Sheets("All Accounts").Index + 1 To Sheets.Count
        Sheets(i).Cells.ClearContents
        Sheets(i).Cells.ClearFormats
    Next
End Sub
 
Upvote 0
You would change
Code:
Sheets(i).Cells.ClearContents
to
Code:
Sheets(i).Cells.Delete


Of course it can be part of another code, the reason for testing it separately is to make sure it works without anything else interfering with it before integrating it in the code.

Put
Code:
Dim irw As Long
with the rest of the lines starting with Dim.

then put the lines below at the point that you want the sheets cleared (you can use F8 to step through your code line by line if you need to see what your code does at each line).

Code:
    For irw = Sheets("All Accounts").Index + 1 To Sheets.Count
        Sheets(irw).Cells.Delete
    Next

Please note that I only changed the variable from i to irw as you are already using, i just to make it less confusing.

Yes, ok we are getting closer! How can I get the code to leave formatting in row 1 on All Accounts alone (those are row headers I want to transfer over to each tab)? Also, how can it leave the column widths alone? When the formula you gave me clears the contents, I only want the data and cell borders to clear or in other words, when the code runs, I want each tab to have the same row 1 header that is on the All Accounts tab and to give each tab the same column widths that exist on All Accounts.
 
Upvote 0

Forum statistics

Threads
1,221,229
Messages
6,158,660
Members
451,507
Latest member
aexis48d

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top