接收到错误后继续下一个文件以在macros中打开

我正在使用这个macros,我离开了这个网站(我认为)。 它转到参考文件夹并打开该文件夹中的所有文件,并复制特定单元格中的信息,并在macros文件的Sheet1中列出它们。 有时它不能find一个文件,我得到一个错误,该文件无法find。 我唯一的select是结束这个macros。 我可以添加什么来使它继续下去,并打开它find的下一个文件? 我正在使用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 

尝试在你的循环中使用这样的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