Deleting Excel worksheets?

From within VB does anyone have any idea how to enumerate through all worksheets in the currently open workbook from the second sheet to the last sheet and delete them all (except the first one)

Either counting them all, selecting them en masse and then deleting them or deleting them individually are both perfectly acceptable...

The worksheets.count method tells you how many sheets are in the current workbook...

Thanks
derek_noonanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

tureCommented:
derek noonan,

From VB, you must set a reference to the MS Excel Object Library. Then you will be able to use code like this:

Sub DeleteSheets()
  dim xl as new excel.application
  dim wb as excel.workbook
  set wb=xl.workbooks.open("c:\test\test.xls"
  xl.application.displayalerts=false
  do while wb.worksheets.count > 1
    wb.worksheets(2).delete
  loop
  xl.application.displayalerts=true
  wb.save
  wb.close
  xl.quit
  set wb=nothing
  set xl=nothing
End Sub

Ture Magnusson
Karlstad, Sweden
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tureCommented:
sorry... I missed a closing parenthesis on the line where the workbook is opened.
0
derek_noonanAuthor Commented:
Thanks for the quick response...

Haven't tried it yet but it looks good and I can tweak it some more anyway

BFN

Derek
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.