Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Worksheet Delete (ActiveWindow.SelectedSheets.Delete)

Posted on 2013-02-03
3
Medium Priority
?
916 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 1200 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 800 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

864 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