我的vba程序必须打开/关闭~150个工作簿。它过去做得很好但突然在执行打开和关闭工作簿的循环时冻结。它适用于较小的套装。
我试图解决的问题:
- 安全模式下的Excel 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
答案 0 :(得分:0)
我觉得它唯一的超级慢,添加一个application.screenupdating = false和enableevents = false
避免公共变量(磁盘??,网络??)
可以使用Long而不是Integer(更快,没有惊喜,超过65k)
如果每次都将工作簿粘贴为函数的参数,为什么你需要它来激活呢?
打开许多工作簿,您可能希望拥有更快的磁盘(SSD)
我知道你使用一个名为sheet的变量作为temp,为什么不在函数结束前设置sheet = nothing?