PDA

View Full Version : Delete if there is a tab



Djblois
08-04-2006, 12:26 PM
Here is my code:

On Error GoTo No_Cust
cust.delete
Set cust = Nothing
No_Cust:
On Error GoTo No_Prod
prod.delete
Set prod = Nothing
No_Prod:
On Error GoTo No_Ship
ship.Delete
Set ship = Nothing
No_Ship:

I want it to delete if there is a TAb with those names and if not go to the next one. However I keep getting an error on the ship.Delete
Please explain what is wrong if it is complicated to figure out.

Daniel

Norie
08-04-2006, 12:51 PM
Daniel

What is ship?

I don't see it defined anywhere in the code, is it a sheet's codename?

Djblois
08-04-2006, 12:58 PM
It is a sheet name. Same as Prod and Cust. I want it to delete Cust if it exists and if not go to Prod. Then Delete Prod if it exists and if not go to Ship. Finally, delete Ship if it exists if not go to my next code.

Daniel

jungix
08-04-2006, 01:03 PM
Are they the only 3 worksheets you have? Because you have to keep at least one, you can't delete all worksheets from a workbook.

Norie
08-04-2006, 01:07 PM
Daniel

So is it a sheet codename?

What error are you getting?

asingh
08-05-2006, 10:45 AM
You could use something like this...it will loop through all your tabs search for the sheet specified...and delete it...

Sub Del_Sheets()

Dim i As Integer
Dim Sheet_Flag As Boolean

Sheet_Flag = False

For i = 1 To Sheets.Count
If Sheets(i).Name = "SHEET_NAME_SPECIFIED" Then
Sheet_Flag = True
End If
Next

If Sheet_Flag = True Then
Application.DisplayAlerts = False
Sheets("SHEET_NAME_SPECIFIED").Delete
Application.DisplayAlerts = True
End If


End sub

Zack Barresse
08-08-2006, 10:20 AM
If that is all you are doing, you can just ignore the error, as it won't delete all worksheets from the workbook (as said, you need at least 1) ...

Dim ws as worksheet, sTmp as string
application.displayalerts = false
on error resume next
For each ws in Thisworkbook.worksheets
select case ws.codename
case "cust", "prod", "ship"
ws.delete
end select
if err <> 0 then
sTmp = ws.name
err.clear
end if
next ws
if stmp <> vbnullstring then
thisworkbook.sheets.add
thisworkbook.sheets(stmp).delete
application.displayalerts = true
end if