user2026021
user2026021

Reputation: 1

Continue to next file to open in macro when an error is received

I am using this macro which I got off this site (I think). It goes to the referenced folder and opens all of the files in that folder and copies the info from certain cells and lists them on Sheet1 within the macro file. Occasionally it cannot find a file and I get an error that the file cannot be found. My only choice is to end the macro. What can I add to this to make it go on and open the next file it does find? I am using Excel 2010.

Thanks!

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

Upvotes: 0

Views: 1472

Answers (1)

L42
L42

Reputation: 19727

Try using Error Handling Routine like this inside your loop.

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

Upvotes: 2

Related Questions