Consulting

Results 1 to 15 of 15

Thread: Excel Crashes when Saving workbook

  1. #1

    Excel Crashes when Saving workbook

    Hello guys,

    I have a problem with a workbook and really do not know how to fix it. I have been looking around Link but no luck. Basically it crashes every time I tried to save it via VBA.

    These are all the things I have tried:

    • Save it manually (works)
    • Using Workbooks.Save (fails)
    • Using Workbooks.SaveAs (fails)
    • Using Workbok.Close true (fails)
    • Removing all ActiveX controls (fails)
    • After using CodeCleaner (fails)


    Any ideas ?

    This is the code I am using; its part of a bigger workbook that updates a sheet in several files. All the other files work.

    Private Sub Update(strFileName, strPassword, strHomeName, intHomeRow, intHomeCol)
    
    
    Dim wbToUpdate As Workbook
    
    
        ' Open the specified macro and make the fund sheet visible
        Set wbToUpdate = Workbooks.Open(strFileName, False, False, , strPassword, strPassword)
        
        With wbToUpdate
            .Sheets(strDataSheet).Visible = True
        
            ' Paste the information
            .Sheets(strDataSheet).Select
            Cells(1, 1).Select
            ActiveSheet.Paste
        
            ' Hide the sheet
            .Sheets(strDataSheet).Visible = xlSheetVeryHidden
        
             
            ' Select the landing Home page and its corresponding cell.
            .Sheets(strHomeName).Select
            Cells(intHomeRow, intHomeCol).Select
        
            ' Save and close the workbook
            
            '.Save
            '.SaveAs strFileName, 52, strPassword, strPassword
            '.Close SaveChanges:=True
            
            .Close
        
        End With
    End Sub
    Thanks a lot for the help
    Feedback is the best way for me to learn


    Follow the Armies

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    hey fredlo.
    i dont know if this is related with the problem. what is strDataSheet?
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3
    Hi,

    That was good question. "strDataSheet" is a module public constant for the name of the sheet where the data will be pasted. I changed it to "m_strDATA_SHEET_NAME" it's better now.
    Feedback is the best way for me to learn


    Follow the Armies

  4. #4
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location


    It might be the fact that you are hiding an active sheet.

    Private Sub Update(strFileName, strPassword, strHomeName, intHomeRow, intHomeCol) 
         
        Dim wbToUpdate As Workbook 
    
         
         ' Open the specified Book and paste the data from the clipboard
        Set wbToUpdate = Workbooks.Open(strFileName, False, False, , strPassword, strPassword) 
           With wbToUpdate 
            .Sheets(m_strDATA_SHEET_NAME). Cells(1, 1).Paste 
    
             
             ' Select the landing Home page and its corresponding cell. Redundant now?
            .Sheets(strHomeName).Cells(intHomeRow, intHomeCol).Select 
           .Close SaveChanges:=True
    End With
    
    End Sub
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  5. #5
    Thanks for the suggestion Sam. I tried that just now and still nothing.
    Feedback is the best way for me to learn


    Follow the Armies

  6. #6
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    This is the code I am using; its part of a bigger workbook that updates a sheet in several files. All the other files work.
    Is the code that works on the other files substantially the same as this code?
    What happens with
       With wbToUpdate 
            .Sheets(m_strDATA_SHEET_NAME). Cells(1, 1).Paste 
             
             Do Events 
    
             ' Select the landing Home page and its corresponding cell. Redundant now?
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  7. #7
    I forgot to mention that I also tried that and no luck.

    Yep, its the same code. The outer Code is basically a loop that goes through a range and gets file names, paths and passwords.

    Thanks for the help Sam.
    Feedback is the best way for me to learn


    Follow the Armies

  8. #8
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    The code works on all but one workbook?
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  9. #9
    Yep!!! but its super old and complicated workbook that needs more than re-factoring. It has about thousands of cell styles, hundreds of conditional formatting. Because the macro keeps added stuff before clearing old data Every time I see it I tremble. :P
    Feedback is the best way for me to learn


    Follow the Armies

  10. #10
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Then the problem is in that workbook.

    Until you get around to refactoring it, you can try a cludge that might work.

    Private Sub Update(strFileName, strPassword, strHomeName, intHomeRow, intHomeCol) 
         
        Dim wbToUpdate As Workbook 
         
         
         ' Open the specified Book and paste the data from the clipboard
        Set wbToUpdate = Workbooks.Open(strFileName, False, False, , strPassword, strPassword) 
        With wbToUpdate 
            .Sheets(m_strDATA_SHEET_NAME). Cells(1, 1).Paste 
             
             
             ' Select the landing Home page and its corresponding cell.
            .Sheets(strHomeName).Cells(intHomeRow, intHomeCol).Select 
    
             If  wbToUpdate.Name = "Old Ugly Workbook Name" Then
               For i = 1 to 1000000
                 For j = 1 to 10000
                   j = j
                 Next j
                  Do Events
               Next i
               msgbox "Closing Mr. ugly now!"
              .Close SaveChanges:=True 
           Else 
             .Close SaveChanges = True
          End If
        End With 
         
    End Sub
    i is big changes in delay, j is small changes. Adjust i until the book always closes, then narrow in on the ideal delay with j. If it works at all.

    With a 1Ghz processor, i*j as above = 10 seconds + DoEvents about every 10 milliseconds
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  11. #11
    I will try this on Monday Sam

    Thanks
    Feedback is the best way for me to learn


    Follow the Armies

  12. #12
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    It is a built-in bottleneck, so put it in your bug tracker.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  13. #13
    Hi Sam,

    I to use the code but still does not work. Hangs in there until it crashes. I just showed the users a message saying that this particular Workbook has to be updated manually.

    Thanks a lot for the help
    Feedback is the best way for me to learn


    Follow the Armies

  14. #14
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Well, I guess you know which workbook needs work on next.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  15. #15
    I know right
    Feedback is the best way for me to learn


    Follow the Armies

Posting Permissions

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