0

循环浏览打开的工作簿

它的代码贯穿所有打开的工作簿,但有一个问题是它运行了大约 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
4

2 回答 2

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
于 2013-01-23T06:26:51.763 回答
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 =字符串以避免任何警告 - 如果需要(但要小心 - 文件将使用“默认”选项打开)并更改源路径,当然)。

于 2013-01-23T11:18:35.427 回答