代码不在所有打开的工作簿上运行
循环打开工作簿
它的代码运行通过所有打开的工作簿,但这是一个问题,它运行约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
为什么要一次打开所有的工作簿? 打开它们,然后closures它们,因为你的工作已经完成。 例如( 未经testing )
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
我在95%的情况下用来循环许多文件的解决scheme – 可能会有所帮助:
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 =
string,以避免任何警告 – 如果需要(但要小心 – 文件将被打开使用“默认”选项),并改变源path,当然)。