Solved

Worksheet Delete (ActiveWindow.SelectedSheets.Delete)

Posted on 2013-02-03
3
692 Views
Last Modified: 2013-05-03
If Sheet 1 gets changed to any other name, but you still have a macro:
Sheets("Sheet1").Select
    ActiveWindow.SelectedSheets.Delete
How would this have to be written to delete sheet 1 no matter what the user names it?
Thanks in advance
0
Comment
Question by:dgd1212
3 Comments
 
LVL 9

Expert Comment

by:chwong67
Comment Utility
Try:
Sheet1.Select
instead of
Sheets("Sheet1").Select
0
 
LVL 14

Accepted Solution

by:
Faustulus earned 300 total points
Comment Utility
The code you are looking for is
Sheets(1).Delete
There is no need to select a sheet in order to do anything with it.

Worksheets have several names. One of them is the name the user gives it. By default, that might be "Sheet1".
Another name is the CodeName. Enter this code in the Immediate Window:
? Sheets("Sheet1").Name, Sheets("Sheet1").CodeName

Open in new window

Both names will be returned as "Sheet1". Then repeat the test after renaming the tab. Now the returned values are "NewName" and "Sheet1". The CodeName hasn't changed.
You can use the CodeName to directly address a worksheet, like
Sheet1.Cells(1,1).Value = "A1"
or
Sheet1.Delete

Open in new window

Finally, each sheet has an Index number which I used in the instruction first above given:
Sheets(1).Delete

Open in new window

. This will delete the first sheet. The first sheet is the sheet whose tab appears to the left. After you delete that sheet another sheet will have the same index number.
If you don't know the name of the sheet the question is what you do know about it. If you have control over the CodeName that would be a sure way of definitely identifying a sheet using VBA. If you know the sheet's tab location within the workbook, the Index will work fine.
0
 
LVL 11

Assisted Solution

by:tel2
tel2 earned 200 total points
Comment Utility
Hi dgd1212,

A slightly more concise alternative to:
    Sheets(1).Delete
that Faustulus mentioned, seems to be:
    Sheets1.Delete
Tested OK in Excel 2003.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now