Results 1 to 3 of 3

Thread: Deleting Sheets in Excel using VBA

Hybrid View

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2003
    Posts
    1

    Deleting Sheets in Excel using VBA

    Hello,

    I am trying to write some code which will allow me to delete the sheets at the end of a workbook automatically when I run a macro. The macro imports a number of worksheets with data in them and this then populates a spreadsheet. Before doing this, I want the macro to search the workbook and delete any sheets which have been imported before importing the new ones.

    The problems I have include:-
    the sheets vary in number from one run to the next.
    the sheets do not have common names.

    Can anyone help?

  2. #2
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863
    So you probably know the "WorkSheet ("WorksheetName").Delete" command. As I understand your problem is to determine the WorksheetName or the Index of it.
    Since you managed to open these sheets during the run of your VBA-Macro (at least that's the way I understood your post) you could store the names of these sheets at that time!

    And if you have the sheets inside the Workbook before the Macro starts, try it with a negative selection. What are the names of the sheets you want to keep, delete all other.
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  3. #3
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    hmmm, just posted a reply on this post which'll be the easiest way to accomplish this:

    http://www.vbforums.com/showthread.p...hreadid=253113

    Either try as suggested above, or store the worksheets.count number into an numeric variable, call your macro, then loop through the sheets & any which are found to have an index greater than this stored number, you can call the worksheets(i).delete method on...

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width