结束没有声明

时间:2016-03-15 16:19:45

标签: excel excel-vba vba

我收到错误结束时没有声明我的代码。基本上我想检查一个文件是否存在,如果没有,那么msgbox如果它然后打开它,复制一个范围,并将范围粘贴到目标范围。我也希望能够为多个文件做到这一点。

Sub test()

Dim WbookCheck As Workbook
Dim filepaths As String

On Error Resume Next
Set WbookCheck = Workbooks("LL-AD-ARROW.xls")
On Error GoTo 0

If WbookCheck Is Nothing Then 'not open....

    filepaths = "c:\Disti-Master\Data\LL-AD-ARROW.xls"

    If Dir(filepaths) = "" Then
        MsgBox "File is missing"
        Exit Sub
    Else
        'file exists - open it
        Set WbookCheck = Workbooks.Open(filepaths)
        Cells.Select
           Sheets("Sheet1").Range("B5").Activate
           Selection.Copy
           Windows("WW Disti Weekly Q116_Master.xlsm").Worksheets("Update").Range("D3").Activate
          Selection.PasteSpecial xlPasteValues
End If
End With
End Sub

1 个答案:

答案 0 :(得分:2)

Sub test()

Dim WbookCheck As Workbook
Dim filepaths As String

On Error Resume Next
Set WbookCheck = Workbooks("LL-AD-ARROW.xls")
On Error GoTo 0

    If WbookCheck Is Nothing Then 'not open....

        filepaths = "c:\Disti-Master\Data\LL-AD-ARROW.xls"

        If Dir(filepaths) = "" Then
            MsgBox "File is missing"
            Exit Sub
        Else
            'file exists - open it
            Set WbookCheck = Workbooks.Open(filepaths)
            Cells.Select
               Sheets("Sheet1").Range("B5").Activate
               Selection.Copy
               Windows("WW Disti Weekly Q116_Master.xlsm").Worksheets("Update").Range("D3").Activate
              Selection.PasteSpecial xlPasteValues
        End If
    End If

End Sub