Consulting

Results 1 to 4 of 4

Thread: Open and close multiple workbooks - out of memory

  1. #1

    Open and close multiple workbooks - out of memory

    Hi,


    I am doing a larger project, where a loop is opening/adding a lot of workbooks, performing some actions, saving and closing the workbooks again.
    This runs as an endless loop on a server, and takes a few minutes per loop.


    Whenever the procedure runs for a specific amount of time, the code fails on the same line, set WB = workbooks.add
    the endless loop can run for a some days before encountering this problem, but a lot of co-workers are relying on the results that are constantly being generated, and I am not always available to reset everything when the error occurs.


    I have been reading alot about forcing the variable to be be released, set WB = Nothing, everytime the file has been closed, but this small sample code keeps running out of memory at around i = 2324:


    Sub Test()
    Application.ScreenUpdating = False
    Dim WB As Workbook
    Dim i As Long
    For i = 1 To 10000
        Set WB = Workbooks.Add
        WB.Close
        Set WB = Nothing
    Next i
    End Sub

    Are there any other ways to force Excel to free up memory, or should I just accept that my program needs to be reset once every few days?


    Best Regards,
    Per Olander

  2. #2
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    Please use code tags around VBA code.

    Sub Test()
       Application.ScreenUpdating = False
    
       For j = 1 To 10000
             with Workbooks.Add
           .Close 0
         end with
       Next
    End Sub
    In a separate (hidden) instance of Excel:

    Sub M_snb()
        With CreateObject("Excel.application")
           For j = 1 To 10000
            With .Workbooks.Add
                .Close 0
            End With
            Next
            .Visible = True
        End With
    End Sub
    Last edited by snb; 09-25-2014 at 02:23 AM.

  3. #3
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    "test" took 89 seconds whereas "M_snb" took 64 seconds on my machine.
    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)

  4. #4
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Hmm, my computer is way slower. For snb's methods I found 3:10 and 1:50.

    The bigger issue is when the user leaves the application to browse the web for example. The foreground method will cause it to hang whereas the background method did not.

    The lesson here is if you can live with the background method, it is best.

Posting Permissions

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