Results 1 to 12 of 12

Thread: [RESOLVED] VB6 Add Excel sheet to workbook if sheet doesn't exist already

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Roodepoort, South Africa
    Posts
    475

    Resolved [RESOLVED] VB6 Add Excel sheet to workbook if sheet doesn't exist already

    I need to create a new Excel workbook from within VB6 program and then write to three different sheets in the workbook.

    On my version of Excel it opens a new workbook with three default sheets namely Sheet1, Sheet2 and Sheet3, so I can rename the sheets and write to them. Works without a problem.

    On another users Excel it opens a new workbook with only one default sheet namely Sheet1, so trying to write to the non existing sheets obviously errors.

    How do I check if the three sheets exist upon creating/opening the workbook and add them if they don't already exist?

  2. #2
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,910

    Re: VB6 Add Excel sheet to workbook if sheet doesn't exist already

    The Workbook has a property .Sheets.Count this can be used to check how many worksheets you have to add.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Roodepoort, South Africa
    Posts
    475

    Re: VB6 Add Excel sheet to workbook if sheet doesn't exist already

    Thanks. Got that sorted now.

    Just another question: When the program erred for instance because there is not three sheets available for writing it goes to the error handler and shows the error message. I then want to exit the sub after closing the Excel instance but it completely locks up my program. I try to close with the following:
    Code:
    oXLBook.close
    Set oXLBook = Nothing
    oXLApp.Quit
    Set oXLApp = Nothing
    Exit Sub
    I have to go to windows Task Manager and end the process there to get the program to close

    Any ideas I can try to prevent the program locking up when some Excel error occurs?

  4. #4
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,444

    Re: VB6 Add Excel sheet to workbook if sheet doesn't exist already

    When you close your Workbook --> with or without saving changes (if the WB is "dirty")?
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Roodepoort, South Africa
    Posts
    475

    Re: VB6 Add Excel sheet to workbook if sheet doesn't exist already

    Without saving. Only saves the workbook once data is written. This crash happened just after creating and opening the workbook when I tried to rename the three sheets I'm going to use. The crash occurred on trying to rename the sheets that doesn't exist (this has been sorted out now so the three sheets are there) but I still would like to know why it locks up the program so badly.

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: VB6 Add Excel sheet to workbook if sheet doesn't exist already

    as you do not specify where in your code the lock up happens it is hard to guess, but as suggested by zvoni excel is presenting a message to ask if you want to save changes, but as you do not see it you do not respond, tell it you do not want to save changes

    Code:
    oXLBook.close false
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Roodepoort, South Africa
    Posts
    475

    Re: VB6 Add Excel sheet to workbook if sheet doesn't exist already

    The lockup happens when trying to rename a sheet that doesn't exist. On my PC Excel opens a workbook with three default worksheets so I renamed the three worksheets to what I need. On another users PC Excel only opens with one default worksheet and the program tried to rename three worksheets, two of which doesn't exist. It then errors and goes to the error handler where I do the above but couldn't exit the program once the error occurred. Will change my close and see how it then goes.

    Thanks for the input.

  8. #8
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,910

    Re: VB6 Add Excel sheet to workbook if sheet doesn't exist already

    How do you rename the sheets?
    You can address them by the index.

    Code:
    Do While oWB.Sheets.Count < 3
      oWB.Sheets.Add
    Loop
    
    For i = 1 To 3
      oWB.Sheets(i).Name = "VBForums " & CStr(i)
    Next i

  9. #9
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: VB6 Add Excel sheet to workbook if sheet doesn't exist already

    Any ideas I can try to prevent the program locking up when some Excel error occurs?
    you need to handle any error that may occur
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  10. #10
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,444

    Re: VB6 Add Excel sheet to workbook if sheet doesn't exist already

    Quote Originally Posted by westconn1 View Post
    as you do not specify where in your code the lock up happens it is hard to guess, but as suggested by zvoni excel is presenting a message to ask if you want to save changes, but as you do not see it you do not respond, tell it you do not want to save changes

    Code:
    oXLBook.close false
    Exactly what i was alluding to.
    The OP says his code jumps to the error-handler, meaning the error itself (renaming missing worksheet) is not the cause for the Lock-up.
    He closes his WB (and misses/ignores the "Do you want so save changes?"-Message).
    His code steps to the next line which tries to quit the Excel-App, which is not possible, since it's still waiting for an answer --> Deadlock

    I'm pretty sure, his workbook is "dirty", since it has at least one sheet he probably renamed succesfully (failing at the second sheet)
    Last edited by Zvoni; Jan 20th, 2022 at 04:28 AM.
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Roodepoort, South Africa
    Posts
    475

    Re: VB6 Add Excel sheet to workbook if sheet doesn't exist already

    I rename them with:
    Code:
    oXLBook.Worksheets(1).Name = "New Name"
    Adding the False to the oXLBook.Close sorted that problem of the lockup. Problem was a Save As from Excel that sat behind my program.

    Thanks for the help.

  12. #12
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,444

    Re: VB6 Add Excel sheet to workbook if sheet doesn't exist already

    Quote Originally Posted by Bezzie View Post
    I rename them with:
    Code:
    oXLBook.Worksheets(1).Name = "New Name"
    Adding the False to the oXLBook.Close sorted that problem of the lockup. Problem was a Save As from Excel that sat behind my program.

    Thanks for the help.
    Exactly what i and pete were telling you
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

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