Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 收到错误时,继续在宏中打开下一个文件_Vba_Excel - Fatal编程技术网

Vba 收到错误时,继续在宏中打开下一个文件

Vba 收到错误时,继续在宏中打开下一个文件,vba,excel,Vba,Excel,我正在使用这个宏,我从这个网站上得到的(我想)。它会转到引用的文件夹,打开该文件夹中的所有文件,从某些单元格复制信息,并将其列在宏文件的Sheet1中。有时它找不到文件,我得到一个错误,文件找不到。我唯一的选择是结束宏。我可以添加什么来继续并打开它找到的下一个文件?我正在使用Excel 2010 谢谢 Sub MergeAllWorkbooks() Dim SummarySheet As Worksheet Dim FolderPath As String Dim SelectedFiles(

我正在使用这个宏,我从这个网站上得到的(我想)。它会转到引用的文件夹,打开该文件夹中的所有文件,从某些单元格复制信息,并将其列在宏文件的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

尝试在循环中使用类似这样的
错误处理例程

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

作为对这个答案的进一步说明,OP,您应该注意,出于保留与手头确切问题相关的答案的原因,Stackoverflow上发布的代码通常不包括重要的错误处理步骤。一般来说,需要添加错误处理和各种鲁棒性检查(例如,处理空值/无值,检查“选择”是一个范围而不是文本框等)。谢谢L42。我试过了,结果出错了。当第一个文件打开时,它表示它已经打开。重新打开它会导致放弃任何更改,我真的想重新打开吗。当我回答“否”时,宏结束。有什么想法吗?