收到错误时,继续下一个文件以宏方式打开

时间:2014-05-09 16:11:39

标签: excel vba excel-vba

我正在使用这个我从这个网站上下载的宏(我想)。它将转到引用的文件夹并打开该文件夹中的所有文件,并复制某些单元格中的信息,并在宏文件中的Sheet1上列出它们。偶尔它找不到文件,我收到一个无法找到该文件的错误。我唯一的选择是结束宏。我可以添加什么来使它继续并打开它找到的下一个文件?我正在使用Excel 2010。

谢谢!

Sub MergeAllWorkbooks()

Dim SummarySheet As Worksheet
Dim FolderPath As String
Dim SelectedFiles() As Variant
Dim NRow As Long
Dim FileName As String
Dim NFile As Long
Dim WorkBk As Workbook
Dim SourceRange As Range
Dim DestRange As Range

' Create a new workbook and set a variable to the first sheet.
Set SummarySheet = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
Columns("C:C").Select
Selection.NumberFormat = "@"

' Modify this folder path to point to the files you want to use.
FolderPath = "X:\billed acct summary shortcut 2014\"

' Set the current directory to the the folder path.
ChDrive FolderPath
ChDir FolderPath

' Open the file dialog box and filter on Excel files, allowing multiple files
' to be selected.
SelectedFiles = Application.GetOpenFilename( _
    filefilter:="Excel Files (*.xls*), *.xls*", MultiSelect:=True)

' NRow keeps track of where to insert new rows in the destination workbook.
NRow = 1

' Loop through the list of returned file names
For NFile = LBound(SelectedFiles) To UBound(SelectedFiles)
    ' Set FileName to be the current workbook file name to open.
    FileName = SelectedFiles(NFile)

    ' Open the current workbook.
    Set WorkBk = Workbooks.Open(FileName)

    ' Set the cell in column A to be the file name.
    SummarySheet.Range("A" & NRow).Value = FileName

    ' Set the source range to be O2 through R2.
    ' Modify this range for your workbooks. It can span multiple rows.
    Set SourceRange = WorkBk.Worksheets(1).Range("E50:M50")

    ' Set the destination range to start at column B and be the same size as the source range.
    Set DestRange = SummarySheet.Range("B" & NRow)
    Set DestRange = DestRange.Resize(SourceRange.Rows.Count, _
       SourceRange.Columns.Count)
    Columns("C:C").Select
    Selection.NumberFormat = "@"

    ' Copy over the values from the source to the destination.
    DestRange.Value = SourceRange.Value

    ' Increase NRow so that we know where to copy data next.
    NRow = NRow + DestRange.Rows.Count

    ' Close the source workbook without saving changes.
    WorkBk.Close savechanges:=False
Next NFile

' Call AutoFit on the destination sheet so that all data is readable.
SummarySheet.Columns.AutoFit

' Sort Macro
Columns("A:M").Select
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("C1:C9"), _
    SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
    .SetRange Range("A1:M1000")
    .Header = xlGuess
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
Columns("G:G").Select
Selection.Delete Shift:=xlToLeft
Range("A1").Select

End Sub

1 个答案:

答案 0 :(得分:2)

尝试在循环中使用Error Handling Routine这样的内容。

For NFile = LBound(SelectedFiles) To UBound(SelectedFiles)
    FileName = SelectedFiles(NFile)
    On Error Resume Next
    Set WorkBk = Workbooks.Open(FileName)
    On Error Goto 0

    If Not WorkBk Is Nothing Then
        '~~> rest of your code here
        .
        .
        WorkBk.Close False

    End If
    Set WorkBk = Nothing
Next