PC Review


Reply
Thread Tools Rate Thread

Delete a worksheet

 
 
=?Utf-8?B?c2NvdHR5ZGVs?=
Guest
Posts: n/a
 
      27th Jul 2007
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
 
Reply With Quote
 
 
 
 
=?Utf-8?B?SmltIFRob21saW5zb24=?=
Guest
Posts: n/a
 
      27th Jul 2007
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

 
Reply With Quote
 
=?Utf-8?B?SGFsaW0=?=
Guest
Posts: n/a
 
      28th Jul 2007
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

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Delete a sheet and delete a module in Another Worksheet then save Trev B Microsoft Excel Programming 4 4th Feb 2010 10:02 PM
delete value on worksheet 2 that match values on worksheet 1 np Microsoft Excel Misc 0 10th Dec 2009 06:02 PM
How to delete a duplicate Excel worksheet within that worksheet? =?Utf-8?B?am96YXd1bg==?= Microsoft Excel Misc 6 19th Sep 2006 02:20 PM
How to delete Excel worksheet using VBA without delete dialog? =?Utf-8?B?Y29jYWxpY28yMg==?= Microsoft Excel Programming 4 15th Sep 2006 10:34 PM
Delete Worksheet and replace/delete links Shaz Microsoft Excel Discussion 3 8th May 2004 12:59 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:15 PM.