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
Solved

Programmatically Creating/Deleting/Renaming Excel Worksheets

Posted on 2000-03-16
2
443 Views
Last Modified: 2008-02-20
I am currently developing a project in Access that takes data and stores it in arrays, and then spits out an Excel Report that can be edited.  What I need to be able to do is delete sheet2 and sheet3 from the newly created object and rename Sheet1.  The following is a snipet of code that I have been using.

   Set ExcelApp = New Excel.Application
   ExcelApp.Visible = True
     
   ExcelApp.Workbooks.Add
   Set Excelbook = ExcelApp.ActiveWorkbook
   
   Set ExcelSheet = Excelbook.Worksheets("Sheet2")

   ExcelSheet.Delete
   ExcelSheet.Activate

this works for deleting but Excel wants an answer if deleting is okay, is there a param I send send like I do when I save.  Also I would like to know how to change the Sheet name from Sheet1 to like Test1.  Thanks in advance, any help is greatly appreciated.

-John
0
Comment
Question by:jforbes
2 Comments
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 100 total points
ID: 2625014
What do you think about that?

Private Sub Command1_Click()
Dim ExcelApp As Excel.Application
Dim ExcelBook As Workbook
Dim intI As Integer

   Set ExcelApp = New Excel.Application
   ExcelApp.Visible = True
   
   ExcelApp.Workbooks.Add
   Set ExcelBook = ExcelApp.ActiveWorkbook
   
   ExcelApp.Application.DisplayAlerts = False
   For intI = ExcelBook.Worksheets.Count To 2 Step -1
        ExcelBook.Worksheets(intI).Delete
   Next intI
   ExcelApp.Application.DisplayAlerts = True

   ExcelBook.Worksheets(1).Name = "New Name"

End Sub
0
 
LVL 1

Author Comment

by:jforbes
ID: 2625136
Thanks Bro!! Works like a charm.
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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

791 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