Looping through opened Workbooks
它的代码贯穿所有已打开的工作簿,但这是一个问题,它运行大约10到12次然后停止。任何人都可以提出任何想法......
Sub OpenAllWorkbooks()
Set destWB = ActiveWorkbook
Dim DestCell As Range
FileNames = Application.GetOpenFilename( _
filefilter:="Excel Files (*.csv*),*.csv*", _
Title:="Select the workbooks to load.", MultiSelect:=True)
If IsArray(FileNames) = False Then
If FileNames = False Then
Exit Sub
End If
End If
For n = LBound(FileNames) To UBound(FileNames)
Set wb = Workbooks.Open(Filename:=FileNames(n), ReadOnly:=True)
Next n
Dim cwb As Workbook
For Each cwb In Workbooks
'With Application
'cwb.AcceptAllChanges
'End With
Call donemovementReport
ActiveWorkbook.Close True
ActiveWorkbook.Close False
Next cwb
End Sub
答案 0 :(得分:2)
为什么一次打开所有工作簿?逐个打开它们,然后在工作完成后关闭它们。例如(未经测试)
Sub OpenAllWorkbooks()
Dim wb As Workbook, thisWb As Workbook
Dim DestCell As Range
Dim FileNames As Variant
Dim n As Long
Set thisWb = ThisWorkbook
FileNames = Application.GetOpenFilename( _
filefilter:="Excel Files (*.csv*),*.csv*", _
Title:="Select the workbooks to load.", MultiSelect:=True)
If IsArray(FileNames) = False Then
If FileNames = False Then
Exit Sub
End If
End If
For n = LBound(FileNames) To UBound(FileNames)
Set wb = Workbooks.Open(Filename:=FileNames(n), ReadOnly:=True)
Call donemovementReport
wb.Close SaveChanges:=True
Next n
End Sub
答案 1 :(得分:0)
我用于在95%的情况下循环许多文件的解决方案 - 可能会有所帮助:
Sub CSV_Cycling()
InputFolder = "D:\DOCUMENTS\"
LoopFileNameExt = Dir(InputFolder & "*.csv")
Do While LoopFileNameExt <> ""
'Application.DisplayAlerts = False
Application.Workbooks.Open (InputFolder & LoopFileNameExt)
'Application.DisplayAlerts = True
[..........YOUR CODE..........]
LoopFileNameExt = Dir
Loop 'Input Folder Files Cycling
End Sub
取消注释Application.DisplayAlerts =
字符串以避免任何警告 - 如果需要(但要小心 - 文件将使用“默认”选项打开)并改变源路径,当然)。