Results 1 to 14 of 14

Thread: [RESOLVED] Kill excel process

  1. #1

    Thread Starter
    Addicted Member alexia_net's Avatar
    Join Date
    Jun 2006
    Posts
    216

    Resolved [RESOLVED] Kill excel process

    Hello! I am trying to delete a worksheet from an *.xls file and then create a different one with the same name. The thing is that excel hangs up during the for sequence. If I mark the For...Next sequence with ' then everything is ok. What is wrong? Please help me. I am desperate. Please check the code below:

    VB Code:
    1. Dim objXLSheet As Object
    2.   Dim ExcelApp As excel.Application
    3.   Dim ExcelBook As excel.Workbook
    4.   Dim ExcelSheet As excel.Worksheets
    5.  
    6.   Dim g_ExcelPath As String
    7.   g_ExcelPath = "C:\listamatforan\1_08A.xls"
    8.   strSheetName = "EXTENDED_PART_LIST"
    9.  
    10. Set ExcelApp = CreateObject("Excel.Application")
    11. With ExcelApp
    12.     '.ExcelBook
    13.     .Workbooks.Open (g_ExcelPath)
    14.    ' Set ExcelSheet = ExcelApp.Worksheets.Add
    15.    ' Set ExcelSheet = ExcelApp.Sheets(1)
    16.  
    17.      'With .ExcelSheet
    18.            If .Worksheets.Count = 1 Then
    19.                  .Worksheets.Add.Name = "DELETE"
    20.                  .Workbooks.Application.SaveWorkspace
    21.            End If
    22.  
    23.      
    24. If .Worksheets.Count > 1 Then
    25.     For buc_sheets = 1 To .Worksheets.Count
    26.         den_sheet = Worksheets(buc_sheets).Name
    27.             If strSheetName = den_sheet Then
    28.                     .Worksheets(strSheetName).Activate
    29.                     .Worksheets(strSheetName).Delete
    30.             End If
    31.     Next
    32.         Workbooks.Close '(g_ExcelPath)
    33.         'Set .Workbooks.Application.ActiveSheet = Nothing
    34. End If
    35.     'End With
    36. End With
    37.  
    38.  
    39. ExcelApp.Application.Quit
    40. Set ExcelApp = Nothing
    Last edited by si_the_geek; Jun 22nd, 2006 at 10:30 AM. Reason: added vbcode tags

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Kill excel process

    Welcome to VBForums!

    There are three mistakes you have made that would cause this problem, the first two of which are the same issue - you have not qualified objects properly.

    The first is in the "den_sheet = " line, you have no . before Worksheets, and the second is another missing . before Workbooks on the "Workbooks.Close" line.

    The third issue is that you do not close the workbook properly - as you have made changes, you need to specify whether you want to save or not. See the Excel Tutorial link in my signature for examples of how to do this.



    There are a few other things I noticed.. for the line "ExcelApp.Application.Quit", there is no need for .Application (as it is the same thing as the ExcelApp object).

    The other is a more significant, and may not cause problems for a while - but will at some ("random") point. You are not using the ExcelBook/ExcelSheet objects you have created, and instead you are just assuming that the correct WorkBook/WorkSheet are being used - which may not be the case (so you could edit/save a completely different WorkBook/WorkSheet that happens to be open).

    For examples of how to use those objects, see the Tutorial again.

  3. #3

    Thread Starter
    Addicted Member alexia_net's Avatar
    Join Date
    Jun 2006
    Posts
    216

    Unhappy Re: Kill excel process

    Hi
    Thank you! )

    Thank you for the "."
    But why my methode for closing the workbooks is not good?


    Quote Originally Posted by si_the_geek
    Welcome to VBForums!

    There are three mistakes you have made that would cause this problem, the first two of which are the same issue - you have not qualified objects properly.

    The first is in the "den_sheet = " line, you have no . before Worksheets, and the second is another missing . before Workbooks on the "Workbooks.Close" line.

    The third issue is that you do not close the workbook properly - as you have made changes, you need to specify whether you want to save or not. See the Excel Tutorial link in my signature for examples of how to do this.



    There are a few other things I noticed.. for the line "ExcelApp.Application.Quit", there is no need for .Application (as it is the same thing as the ExcelApp object).

    The other is a more significant, and may not cause problems for a while - but will at some ("random") point. You are not using the ExcelBook/ExcelSheet objects you have created, and instead you are just assuming that the correct WorkBook/WorkSheet are being used - which may not be the case (so you could edit/save a completely different WorkBook/WorkSheet that happens to be open).

    For examples of how to use those objects, see the Tutorial again.

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Kill excel process

    Do it manually and see what happens when you try to close a workbook that has been edited... you get a message asking if you want to save it or not (and the program wont close while the message is there). Exactly the same thing happens when using code, except you cannot see the message (or automatically click on it).

    You therefore need to explicitly state whether you want to save it or not - and the ways of doing that are shown in the tutorial.

  5. #5

    Thread Starter
    Addicted Member alexia_net's Avatar
    Join Date
    Jun 2006
    Posts
    216

    Re: Kill excel process

    But this command
    .Workbooks.Application.SaveWorkspace
    saves the workbook
    I have checked.

    Quote Originally Posted by si_the_geek
    Do it manually and see what happens when you try to close a workbook that has been edited... you get a message asking if you want to save it or not (and the program wont close while the message is there). Exactly the same thing happens when using code, except you cannot see the message (or automatically click on it).

    You therefore need to explicitly state whether you want to save it or not - and the ways of doing that are shown in the tutorial.

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Kill excel process

    Erm.. there are two problems with that.

    First of all, it does not save the Workbook. What it saves is a list of the Workbooks that are open (and their positions etc) so that you can load the same files in one go the next time you open Excel (MS explanation).

    The second problem is that you make changes to the workbook after that "save", so you need to save again - the save must be done after all editing work you do with the file (but before [or at] the .Close).


    As mentioned before, the valid methods for saving are in my tutorial (post #5).

  7. #7

    Thread Starter
    Addicted Member alexia_net's Avatar
    Join Date
    Jun 2006
    Posts
    216

    Re: Kill excel process

    I do not know what to do. I am new with this. I have a total fog in my head.

    Quote Originally Posted by si_the_geek
    Erm.. there are two problems with that.

    First of all, it does not save the Workbook. What it saves is a list of the Workbooks that are open (and their positions etc) so that you can load the same files in one go the next time you open Excel (MS explanation).

    The second problem is that you make changes to the workbook after that "save", so you need to save again - the save must be done after all editing work you do with the file (but before [or at] the .Close).


    As mentioned before, the valid methods for saving are in my tutorial (post #5).

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Kill excel process

    Here is an updated version, with a couple of extra 'fixes' that I found when I indented the code properly:
    VB Code:
    1. Dim ExcelApp As Excel.Application
    2. Dim ExcelBook As Excel.Workbook
    3. 'Dim ExcelSheet As Excel.Worksheets  '(not needed for the code you have)
    4. Dim g_ExcelPath As String
    5.  
    6.   g_ExcelPath = "C:\listamatforan\1_08A.xls"
    7.   strSheetName = "EXTENDED_PART_LIST"
    8.  
    9.   Set ExcelApp = CreateObject("Excel.Application")
    10. 'use object variables (to ensure we are working with the right workbook)
    11.   Set ExcelBook = ExcelApp.Workbooks.Open(g_ExcelPath)
    12.  
    13. 'work with the specific workbook
    14.   With ExcelBook
    15.     If .Worksheets.Count = 1 Then
    16.       .Worksheets.Add.Name = "DELETE"
    17. 'SaveWorkspace removed - as it is not relevant (it does not save the file)
    18.      
    19. 'this If changed to ElseIf, as previously this would always be true (the If block above adds a sheet if there is only 1) This may not be what you want!
    20.     ElseIf .Worksheets.Count > 1 Then
    21.       For buc_sheets = 1 To .Worksheets.Count
    22.         den_sheet = .Worksheets(buc_sheets).Name
    23.         If strSheetName = den_sheet Then
    24.           .Worksheets(strSheetName).Activate
    25.           .Worksheets(strSheetName).Delete
    26.           Exit For  '(as we have found the sheet, no point checking others - they cant have the same name)
    27.         End If
    28.       Next
    29.     End If
    30.   End With
    31. 'moved outside of the if block (so it is always executed), and altered so it saves the file
    32.   ExcelBook.Close SaveChanges:=True
    33.   Set ExcelBook = Nothing
    34.  
    35. 'corrected
    36.   ExcelApp.Quit
    37.   Set ExcelApp = Nothing

  9. #9

    Thread Starter
    Addicted Member alexia_net's Avatar
    Join Date
    Jun 2006
    Posts
    216

    Re: Kill excel process

    Thank you a milion times. You'he just made my day shine. I see what I was doing wrong now. Thank you very much again!


    Quote Originally Posted by si_the_geek
    Here is an updated version, with a couple of extra 'fixes' that I found when I indented the code properly:
    VB Code:
    1. Dim ExcelApp As Excel.Application
    2. Dim ExcelBook As Excel.Workbook
    3. 'Dim ExcelSheet As Excel.Worksheets  '(not needed for the code you have)
    4. Dim g_ExcelPath As String
    5.  
    6.   g_ExcelPath = "C:\listamatforan\1_08A.xls"
    7.   strSheetName = "EXTENDED_PART_LIST"
    8.  
    9.   Set ExcelApp = CreateObject("Excel.Application")
    10. 'use object variables (to ensure we are working with the right workbook)
    11.   Set ExcelBook = ExcelApp.Workbooks.Open(g_ExcelPath)
    12.  
    13. 'work with the specific workbook
    14.   With ExcelBook
    15.     If .Worksheets.Count = 1 Then
    16.       .Worksheets.Add.Name = "DELETE"
    17. 'SaveWorkspace removed - as it is not relevant (it does not save the file)
    18.      
    19. 'this If changed to ElseIf, as previously this would always be true (the If block above adds a sheet if there is only 1) This may not be what you want!
    20.     ElseIf .Worksheets.Count > 1 Then
    21.       For buc_sheets = 1 To .Worksheets.Count
    22.         den_sheet = .Worksheets(buc_sheets).Name
    23.         If strSheetName = den_sheet Then
    24.           .Worksheets(strSheetName).Activate
    25.           .Worksheets(strSheetName).Delete
    26.           Exit For  '(as we have found the sheet, no point checking others - they cant have the same name)
    27.         End If
    28.       Next
    29.     End If
    30.   End With
    31. 'moved outside of the if block (so it is always executed), and altered so it saves the file
    32.   ExcelBook.Close SaveChanges:=True
    33.   Set ExcelBook = Nothing
    34.  
    35. 'corrected
    36.   ExcelApp.Quit
    37.   Set ExcelApp = Nothing

  10. #10
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Kill excel process

    No problem.


    As you have the answer, could you please do us a little favour, and mark this thread as Resolved?
    (this saves time reading for those of us who like to answer questions, and also helps those who search to find answers)

    You can do this by clicking on "Thread tools" just above the first post in this thread, then "Mark thread resolved".

  11. #11

    Thread Starter
    Addicted Member alexia_net's Avatar
    Join Date
    Jun 2006
    Posts
    216

    Re: Kill excel process

    ok. I did it. Thanks again!

    Quote Originally Posted by si_the_geek
    No problem.


    As you have the answer, could you please do us a little favour, and mark this thread as Resolved?
    (this saves time reading for those of us who like to answer questions, and also helps those who search to find answers)

    You can do this by clicking on "Thread tools" just above the first post in this thread, then "Mark thread resolved".

  12. #12

    Thread Starter
    Addicted Member alexia_net's Avatar
    Join Date
    Jun 2006
    Posts
    216

    Unhappy Re: Kill excel process

    However there is a small problem in this program. Or maybe I am doing something wrong. If the sheet I want to delete is not the first the program won't delete it. At least this is what is happening on my computer. I was testing the program and I was surprised when i have seen that the program did not delete the worksheet. As if it couldn't make it active. I have replaced the first sheet with the one I wanted to delete and it was ok. Why can't the program make it active?

    Quote Originally Posted by si_the_geek
    No problem.


    As you have the answer, could you please do us a little favour, and mark this thread as Resolved?
    (this saves time reading for those of us who like to answer questions, and also helps those who search to find answers)

    You can do this by clicking on "Thread tools" just above the first post in this thread, then "Mark thread resolved".

  13. #13
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [RESOLVED] Kill excel process

    Ah yes, I'd forgotten about this - it's another one that shows a confirmation message.. unfortunatley you can't specify the option here, but you can temporarily disable confirmation messages (only temporarily tho, otherwise they wont be shown when the user does similar things).

    Two lines of code need to be added like this:
    VB Code:
    1. ExcelApp.DisplayAlerts = False
    2.           .Worksheets(strSheetName).Delete
    3.           ExcelApp.DisplayAlerts = True

  14. #14

    Thread Starter
    Addicted Member alexia_net's Avatar
    Join Date
    Jun 2006
    Posts
    216

    Re: [RESOLVED] Kill excel process

    Thank you a 1.000.000. times more for your help. My best regards!

    Quote Originally Posted by si_the_geek
    Ah yes, I'd forgotten about this - it's another one that shows a confirmation message.. unfortunatley you can't specify the option here, but you can temporarily disable confirmation messages (only temporarily tho, otherwise they wont be shown when the user does similar things).

    Two lines of code need to be added like this:
    VB Code:
    1. ExcelApp.DisplayAlerts = False
    2.           .Worksheets(strSheetName).Delete
    3.           ExcelApp.DisplayAlerts = True

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