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?
 
Thanks Michael:
I did not remember this line of his post:
Excel deletes all tabs to the right of my data sheet.
 
Upvote 0

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
I just want to clear the sheets the macro creates or only clear sheets to the right of the All Accounts tab. Is that doable?
 
Upvote 0
Install the script I made and call it this way: Note text in "Red"

Code:
Sub SplitDivisions()

 Dim ws As Worksheet
 Dim wsOut As Worksheet
 Dim i As Long
 Dim lr As Long
 Dim TabName As String
 Dim Found As Boolean
[COLOR="#FF0000"]Call Clear_All_Sheets[/COLOR]
 With ThisWorkbook.Worksheets("All Accounts")
 For i = 2 To .Cells(.Rows.Count, "H").End(xlUp).Row
 TabName = CStr(.Cells(i, "H"))
 Found = False
 For Each ws In Worksheets
 If ws.Name = TabName Then Found = True
 Next
 If Found Then
 Set wsOut = Worksheets(TabName)
 Else
 Set wsOut = ThisWorkbook.Worksheets.Add(After:=Worksheets(Worksheets.Count))
 wsOut.Name = TabName
 .Range("A1:Q1").Copy
 With wsOut.Range("A1:Q1")
 .PasteSpecial xlPasteAll
 .PasteSpecial xlPasteColumnWidths
 End With
 End If
 lr = wsOut.Cells(wsOut.Rows.Count, "H").End(xlUp).Row
 .Range("A1:Q1").Offset(i - 1).Copy
 wsOut.Range("A1:Q1").Offset(lr).PasteSpecial xlPasteAll
 Next
 End With

 End Sub

This code is not clearing the existing data.
 
Upvote 0
did you try my code ??.....and change the sheet name "Master" to "All Accounts"
Rich (BB code):
Sub SplitDivisions()

 Dim ws As Worksheet
 Dim wsOut As Worksheet
 Dim i As Long
 Dim lr As Long
 Dim TabName As String
 Dim Found As Boolean
 With ThisWorkbook.Worksheets("All Accounts")
 For i = 2 To .Cells(.Rows.Count, "H").End(xlUp).Row
 TabName = CStr(.Cells(i, "H"))
 Found = False
 For Each ws In Worksheets
 If ws.Name = TabName Then Found = True
 Next
 If Found Then
 Set wsOut = Worksheets(TabName)
 Else
 Set wsOut = ThisWorkbook.Worksheets.Add(After:=Worksheets(Worksheets.Count))
 wsOut.Name = TabName
 .Range("A1:Q1").Copy
 With wsOut.Range("A1:Q1")
 .PasteSpecial xlPasteAll
 .PasteSpecial xlPasteColumnWidths
 End With
 End If
 lr = wsOut.Cells(wsOut.Rows.Count, "H").End(xlUp).Row
 .Range("A1:Q1").Offset(i - 1).Copy
 wsOut.Range("A1:Q1").Offset(lr).PasteSpecial xlPasteAll
 Next
 End With
Call Clear_All_Sheets
 End Sub
 
Last edited:
Upvote 0
did you try my code ??.....and change the sheet name "Master" to "All Accounts"
Rich (BB code):
Sub SplitDivisions()

 Dim ws As Worksheet
 Dim wsOut As Worksheet
 Dim i As Long
 Dim lr As Long
 Dim TabName As String
 Dim Found As Boolean
 With ThisWorkbook.Worksheets("All Accounts")
 For i = 2 To .Cells(.Rows.Count, "H").End(xlUp).Row
 TabName = CStr(.Cells(i, "H"))
 Found = False
 For Each ws In Worksheets
 If ws.Name = TabName Then Found = True
 Next
 If Found Then
 Set wsOut = Worksheets(TabName)
 Else
 Set wsOut = ThisWorkbook.Worksheets.Add(After:=Worksheets(Worksheets.Count))
 wsOut.Name = TabName
 .Range("A1:Q1").Copy
 With wsOut.Range("A1:Q1")
 .PasteSpecial xlPasteAll
 .PasteSpecial xlPasteColumnWidths
 End With
 End If
 lr = wsOut.Cells(wsOut.Rows.Count, "H").End(xlUp).Row
 .Range("A1:Q1").Offset(i - 1).Copy
 wsOut.Range("A1:Q1").Offset(lr).PasteSpecial xlPasteAll
 Next
 End With
Call Clear_All_Sheets
 End Sub

Yep. It is adding to the existing code and not clearing old data.
 
Upvote 0
The script I wrote should clear all the sheets in your workbook. Now if you do not want a particular sheet to be cleared then notice Michaels modified script of mine. You must have the script installed in the workbook.
I always test my scripts.
 
Upvote 0
I believe the:
Call Clear_All_Sheets
Needs to be at the beginning of the script not at the end.
 
Upvote 0
The script I put in post # 9 is what you should use.
You only need that one line of code I entered in Red
When the script gets to that line it runs the script called Clear_All_Sheets
Now my script clears all sheets if you want your Master sheet not cleared then I need to know the name of the sheet you do not want cleared. Is the sheet named "Master"?
 
Upvote 0
I believe the:
Call Clear_All_Sheets
Needs to be at the beginning of the script not at the end.

Forgive me but I am not sure what to do with the Call Clear_All_Sheets. When I run the code, I get a debug error on that line.

I am a beginner to VBA.
 
Upvote 0

Forum statistics

Threads
1,221,230
Messages
6,158,662
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