Solved

Worksheet Delete (ActiveWindow.SelectedSheets.Delete)

Posted on 2013-02-03
3
765 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
ID: 38849597
Try:
Sheet1.Select
instead of
Sheets("Sheet1").Select
0
 
LVL 14

Accepted Solution

by:
Faustulus earned 300 total points
ID: 38849605
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 12

Assisted Solution

by:tel2
tel2 earned 200 total points
ID: 38849609
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

680 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