floridagunner
Board Regular
- Joined
- Jul 20, 2007
- Messages
- 60
Hello,
I need a macro that will hide sheet tabs with certain names as well as delete other sheets. However when it comes to processing the code where it has to check for sheet names and hide or leave alone, I am running into an error.
Below is the code I'm using:
Sub DeleteSheets2()
''Delete all worksheets except sheet 1
Dim ws As Worksheet
Application.DisplayAlerts = False
For Each ws In Worksheets
If ws.Name <> "Sheet1" And ws.Name <> "Variance Evaluation" And ws.Name <> "Analysis" And ws.Name <> "Device Removal Pivot Table" Then ws.Delete
Next
Application.DisplayAlerts = True
If ws.Name = "Variance Evaluation" Then ws.Visible = False
If ws.Name = "Analysis" Then ws.Visible = False
If ws.Name = "Device Removal Pivot Table" Then ws.Visible = False
''Add worksheets
Sheets("Sheet1").Select
Sheets.Add
ActiveSheet.Name = "Sheet3"
Sheets.Add
ActiveSheet.Name = "Sheet2"
Sheets("Sheet1").Select
Sheets("Sheet1").Move Before:=Sheets(1)
End Sub
Can anyone tell me what I'm doing wrong?
Thanks
I need a macro that will hide sheet tabs with certain names as well as delete other sheets. However when it comes to processing the code where it has to check for sheet names and hide or leave alone, I am running into an error.
Below is the code I'm using:
Sub DeleteSheets2()
''Delete all worksheets except sheet 1
Dim ws As Worksheet
Application.DisplayAlerts = False
For Each ws In Worksheets
If ws.Name <> "Sheet1" And ws.Name <> "Variance Evaluation" And ws.Name <> "Analysis" And ws.Name <> "Device Removal Pivot Table" Then ws.Delete
Next
Application.DisplayAlerts = True
If ws.Name = "Variance Evaluation" Then ws.Visible = False
If ws.Name = "Analysis" Then ws.Visible = False
If ws.Name = "Device Removal Pivot Table" Then ws.Visible = False
''Add worksheets
Sheets("Sheet1").Select
Sheets.Add
ActiveSheet.Name = "Sheet3"
Sheets.Add
ActiveSheet.Name = "Sheet2"
Sheets("Sheet1").Select
Sheets("Sheet1").Move Before:=Sheets(1)
End Sub
Can anyone tell me what I'm doing wrong?
Thanks