?
Solved

Worksheet Delete (ActiveWindow.SelectedSheets.Delete)

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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!
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
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.

762 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