Delete sheet without prompt

C

chad

How can I delete a sheet without a message box prompting for the user to confirm the deletion of the sheet? Also, is there a way to rename a sheet without it defaulting to "sheet1" or the next number? In other words, if "sheet1" already exists when I add a new sheet it will default to "sheet2" and the following code will generatte an error

Sheets("Import").Selec
ActiveWindow.SelectedSheets.Delet
Sheets.Ad
Sheets("Sheet1").Name = "Import

Thank
 
R

Ron de Bruin

Hi chad

Application.DisplayAlerts = False
'delete code
Application.DisplayAlerts = True


This example will not give a error if it exist

On Error Resume Next
Sheets("Sheet1").Name = "Import"
On Error GoTo 0



--
Regards Ron de Bruin
http://www.rondebruin.nl


chad said:
How can I delete a sheet without a message box prompting for the user to confirm the deletion of the sheet? Also, is there a way
to rename a sheet without it defaulting to "sheet1" or the next number? In other words, if "sheet1" already exists when I add a new
sheet it will default to "sheet2" and the following code will generatte an error.
 
B

Bob Phillips

Here is one way for the second part

iNext = Activeworkbook.Worksheets.Count + 1
Do Until Not SheetExists("Sheet" & iNext)
iNext = iNext + 1
Loop
Woeksheets.Add.Name = "Sheet" & iNext



Function SheetExists(Filename As String)
Dim oSh As Worksheet
On Error Resume Next
Set oSh = ActiveWorkbook.Worksheets(Filename)
On Error GoTo 0
SheetExists = Not oSh Is Nothing
End Function


--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

chad said:
How can I delete a sheet without a message box prompting for the user to
confirm the deletion of the sheet? Also, is there a way to rename a sheet
without it defaulting to "sheet1" or the next number? In other words, if
"sheet1" already exists when I add a new sheet it will default to "sheet2"
and the following code will generatte an error.
 
Top