Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deleting Excel Spreadsheets

Status
Not open for further replies.

WOTRAC

MIS
Nov 22, 2002
36
GB
I have a workbook containing only 1 worksheet.
When I open the workbook, it automatically adds a number of worksheets based on a recordset obtained from a database.

I want to be able to delete all of these new worksheets, when I close the workbook, leaving only the original worksheet.

Can someone advise me on the best way to achieve this.

 
Hi,

Your existing worksheet has a name, let's say it is "MySheet"

This code is ing the ThisWorkbook Object Code...
Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
  for each ws in worksheets
    with ws
      if .name <> "MySheet" then
        .delete
      end if
    end with
  next
End Sub



Skip,

Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Thanks

I never thought about doing it that way, I was obviously making it too hard for myself.


Paul
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top