Results 1 to 4 of 4

Thread: Created Excel Worksheet always becomes the 1st worksheet

  1. #1

    Thread Starter
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Created Excel Worksheet always becomes the 1st worksheet

    Why does that happen. I simply want any new worksheet to join the existing worksheets but after the existing worksheets.

    VB Code:
    1. Dim FileName As String
    2.         Dim UserPassword As String
    3.         UserPassword = "letmein"
    4.  
    5.         FileName = textbox1.text
    6.  
    7.         Dim objExcel As New Excel.Application
    8.         Dim oExcelWB As Excel.Workbook
    9.         Dim oExcelWS As Excel.Worksheet
    10.  
    11.         objExcel = CType(CreateObject("Excel.Application"), Excel.Application)
    12.         objExcel.Visible = False
    13.         oExcelWB = objExcel.Workbooks.Add()
    14.        
    15.         'RENAME EXISTING 3 WORKSHEETS
    16.         objExcel.Worksheets(1).name = "OPTION 1"
    17.         objExcel.Worksheets(2).name = "OPTION 2"
    18.         objExcel.Worksheets(3).name = "OPTION 3"
    19.  
    20.         'ADD NEW WORKSHEET
    21.         oExcelWS = objExcel.Worksheets.Add()
    22.         oExcelWS.Name = "OPTION 4"
    23.  
    24.    oExcelWB.Close(True, "D:\My Documents\" & FileName & ".xls")
    25.         oExcelWB = Nothing

    When I open the excel workbook the order is - Option 4 - Option 3 - etc...

    I need it to go 1 - 2 - 3 - 4
    Last edited by stimbo; Jul 27th, 2006 at 03:52 AM.

  2. #2

    Thread Starter
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: Created Excel Worksheet always becomes the 1st worksheet

    Bump

  3. #3
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: Created Excel Worksheet always becomes the 1st worksheet

    To answer your question:

    VB Code:
    1. oExcelWS = objExcel.Worksheets.Add(After:=objExcel.Worksheets(3))

    However, there are additional issues with your code that are going to cause you headaches in the future:

    1.) You are creating two instances of the Excel application that will result extra memory usage.
    2.) You are not properly cleaning up your excel objects, which will result in memory leaks and additional processes being left open.

    To correct #1, take out the
    VB Code:
    1. objExcel = CType(CreateObject("Excel.Application"), Excel.Application)
    line. You already have an Excel.Application when you did your Dim objExcel as New Excel.Application.

    To Correct #2, you need to be sure you add the following code after all of your code executes:

    VB Code:
    1. objExcel.Quit()
    2.  
    3.         Marshal.ReleaseComObject(oExcelWB)
    4.         Marshal.ReleaseComObject(oExcelWS)
    5.         Marshal.ReleaseComObject(objExcel)
    6.  
    7.         oExcelWB = Nothing
    8.         oExcelWS = Nothing
    9.         objExcel = Nothing

  4. #4

    Thread Starter
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: Created Excel Worksheet always becomes the 1st worksheet

    Thank you for the info, very useful indeed.

    I spotted the 1st issue a little after I had posted when i tried to shut down my computer and realised excel apps had been loaded and not unloaded - got about 10 prompts for various things!

    Program is pretty much done now, I got the rest working eventually.

    Thanks again,

    S.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width