簡體   English   中英

Excel / VBA在執行打開/關閉工作簿的循環期間凍結

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

我的vba程序必須打開/關閉約150個工作簿。 它曾經做得很好,但是突然間在打開和關閉工作簿的循環執行過程中凍結。 它適用於較小的設備。

我嘗試解決的問題:

-在安全模式下運行Excel。

-減小了我最大模塊的尺寸

-修復的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

我認為這只是超級慢,添加一個application.screenupdating = false並與enableevents = false相同

避免使用公共變量(磁盤,網絡)

可以使用Long而不是Integer(速度更快,超過65k不會令人驚訝)

如果工作簿每次都粘貼為函數的參數,為什么還要激活它呢?

打開許多工作簿時,您可能希望擁有更快的磁盤(SSD)

我知道您使用一個名為sheet的變量作為temp,為什么不在函數結束前設置sheet = nothing?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM