Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Worksheet Delete (ActiveWindow.SelectedSheets.Delete)

Posted on 2013-02-03
3
Medium Priority
?
889 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
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 demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

618 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