简体   繁体   中英

Excel/VBA freezes during the loop that executes opening/closing of workbooks

My vba program has to open/close ~150 workbooks. It used to do it fine but suddenly it freezes during execution of the loop that opens and closes the workbooks. It works fine on smaller sets.

Things I have tried to resolve issue:

-Ran Excel in safe mode.

-Reduced the size of my largest module

-Repaired Excel.

    Function loopXls(dirStr As String) As collection
        Dim count As Integer
        Dim strFilename As String
        Dim strPath As String
        Dim wbkTemp As Workbook
        Dim sheet As layer7sheet
        Dim sheets As collection


       Function loopXls(dirStr As String) As collection
    Dim count As Integer
    Dim strFilename As String
    Dim strPath As String
    Dim wbkTemp As Workbook
    Dim sheet As layer7sheet
    Dim sheets As collection
    Dim debugCount As Integer


    strPath = "C:\Users\pmevi\Documents\L7\L7_Master_Book\Input\" & dirStr & "\"
    strFilename = dir(strPath & "*.xls")
    Set sheets = New collection
    Do While Len(strFilename) > 0
        debugCount = debugCount + 1
        Set wbkTemp = Workbooks.Open(strPath & strFilename)

        '
        ' do your code with the workbook
        '
        'Set sheets = New collection
        Set sheet = New layer7sheet

        Set sheet = getCPUsheet(wbkTemp)
'        Debug.Print ("cpu avg is " & sheet.getAvg)
'        Debug.Print ("cpu max is " & sheet.getMax)
        sheets.Add sheet

             Set sheet = New layer7sheet
        Set sheet = getDiskSheet(wbkTemp)
'        Debug.Print ("disk avg is " & sheet.getAvg)
'        Debug.Print ("disk max is " & sheet.getMax)
        sheets.Add sheet

        Set sheet = New layer7sheet
        Set sheet = getMemorySheet(wbkTemp)
'        Debug.Print ("memory avg is " & sheet.getAvg)
'        Debug.Print ("memory max is " & sheet.getMax)
        sheets.Add sheet
        Set sheet = New layer7sheet
        Set sheet = getNetworkSheet(wbkTemp)
'        Debug.Print ("network avg is " & sheet.getAvg)
'        Debug.Print ("network max is " & sheet.getMax)
        sheets.Add sheet
        Dim debugName As String
        debugName = sheet.getWorkbookName
        Log debugCount & " " & debugName, "C:\Users\pmevi\Documents\L7\L7_Master_Book\log.txt"
        wbkTemp.Close True

        strFilename = dir
    Loop
    'Debug.Print ("sheets count: " & sheets.count)
    Set loopXls = sheets

End Function

Function getCPUsheet(wrkbook As Workbook) As layer7sheet
    Dim sheet As layer7sheet
    Dim str As String
    str = reduceString(wrkbook.name)
    Set sheet = New layer7sheet
    wrkbook.Activate
    sheet.setWorkbookName = (str)
    sheet.setSheetType = CPU
    sheet.setavg = calculateAverage(wrkbook.name, "CPU")
    sheet.setMax = calculateMax(wrkbook.name, "CPU")
    Set getCPUsheet = sheet
End Function

Function getMemorySheet(wrkbook As Workbook) As layer7sheet
    Dim sheet As layer7sheet
    Set sheet = New layer7sheet
    wrkbook.Activate
     Dim str As String
    str = reduceString(wrkbook.name)
    sheet.setWorkbookName = str
    sheet.setSheetType = memory
    sheet.setavg = calculateAverage(wrkbook.name, "memory")
    sheet.setMax = calculateMax(wrkbook.name, "memory")
    Set getMemorySheet = sheet
End Function

Function getDiskSheet(wrkbook As Workbook) As layer7sheet
    Dim sheet As layer7sheet
    Set sheet = New layer7sheet
    wrkbook.Activate
     Dim str As String
    str = reduceString(wrkbook.name)
    sheet.setWorkbookName = str
    sheet.setSheetType = disk
    sheet.setavg = calculateAverage(wrkbook.name, "disk")
    sheet.setMax = calculateMax(wrkbook.name, "disk")
    Set getDiskSheet = sheet
End Function

Function getNetworkSheet(wrkbook As Workbook) As layer7sheet
    Dim sheet As layer7sheet
    Set sheet = New layer7sheet
    wrkbook.Activate
     Dim str As String
    str = reduceString(wrkbook.name)
    sheet.setWorkbookName = str
    sheet.setSheetType = network
    sheet.setavg = calculateAverage(wrkbook.name, "network")
    sheet.setMax = calculateMax(wrkbook.name, "network")
    Set getNetworkSheet = sheet
End Function

i think its only uber slow, add a application.screenupdating=false and same with enableevents=false

avoid public variables (disk ?? , network ??)

could use Long instead of Integer (faster, no surprises with over 65k)

if workbook is paste each time as argument for your function, why the hell you need to .activate it ?

with opening many Workbooks, you might want to have a faster disk (SSD)

i understand you use a variable named sheet as temp, why not set sheet=nothing before the end of your functions ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM