ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Delete a worksheet (https://www.excelbanter.com/excel-programming/394316-delete-worksheet.html)

scottydel

Delete a worksheet
 
Hello,

I'm using Excel 2003. I'm having trouble deleting a worksheet with VBA.
The code looks fine, even used MSDN to find some examples:

Worksheets("Sheet2").Delete

But it's not working. I open an Excel file in VBA, I add a sheet, copy data
from the sheet to another sheet, then delete the sheet, and save and close
the workbook. All of this is done with VBA. When I open up the workbook
manually, the sheet I thought I just deleted is still there. The copy and
paste are working fine.

Is there some prep-work that might need to take place before Excel allows
you to delete a sheet?

Any ideas?

Thanks,

Scott

Jim Thomlinson

Delete a worksheet
 
That code is just fine. Post the rest of your code so that we can see what
you are up to. As a guess your problem is in referencing the workbook that
you opened. By any chance do you have On Error Resume Next in your code. If
the sheet is not getting deleted then it should be throwing an error.
Additionally if it is going to delete it will generate and alert that you are
about to delete a sheet (unless you have turned DispayAlerts off)...
--
HTH...

Jim Thomlinson


"scottydel" wrote:

Hello,

I'm using Excel 2003. I'm having trouble deleting a worksheet with VBA.
The code looks fine, even used MSDN to find some examples:

Worksheets("Sheet2").Delete

But it's not working. I open an Excel file in VBA, I add a sheet, copy data
from the sheet to another sheet, then delete the sheet, and save and close
the workbook. All of this is done with VBA. When I open up the workbook
manually, the sheet I thought I just deleted is still there. The copy and
paste are working fine.

Is there some prep-work that might need to take place before Excel allows
you to delete a sheet?

Any ideas?

Thanks,

Scott


Halim

Delete a worksheet
 
hi,


I hope this will help to expand your code...

Sub CopyDataThenDelSht()
Dim ShtSource As Worksheet
Dim ShtDest As Worksheet

'setting variables sheet
Set ShtSource = Sheets("Sheet1")
Set ShtDest = Sheets("Sheet2")

'Copy data
ShtSource.Range("A1").Copy _
Destination:=ShtDest.Range("A1")

'turnoff msgbox delete confirmation
Application.DisplayAlerts = False
ShtSource.Delete
'turnon msgbox delete confirmation
Application.DisplayAlerts = True

'''''Then Save the workbook
'''It's important to make permanently deleted
'''make sure before you want to permanently delete
'ShtSource.Parent.Save
End Sub
..

That's all samples
--
Regards,

Halim



"scottydel" wrote:

Hello,

I'm using Excel 2003. I'm having trouble deleting a worksheet with VBA.
The code looks fine, even used MSDN to find some examples:

Worksheets("Sheet2").Delete

But it's not working. I open an Excel file in VBA, I add a sheet, copy data
from the sheet to another sheet, then delete the sheet, and save and close
the workbook. All of this is done with VBA. When I open up the workbook
manually, the sheet I thought I just deleted is still there. The copy and
paste are working fine.

Is there some prep-work that might need to take place before Excel allows
you to delete a sheet?

Any ideas?

Thanks,

Scott



All times are GMT +1. The time now is 09:57 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com