即使在取消之后,Excel VBA也会继续“打开文件”

好的,在这里。 在过去的3到4个月里,我已经完成了一些编码,学到了很多东西,但是我不知道为什么这个代码仍然打开一个文件,当我popup一个popup窗口,过滤的文件名。 任何意见将不胜感激。

Sub OpenByPartialName() ' Returns popup window with only filtered filenames matching ' Partial Filename input Dim WB As Workbook Dim Ans As String Dim MyFile As String Dim path As String ' Folder Path Name for Forms path = ("S:\Forms Folder\") Ans = InputBox("Enter Partial filename Filter", "Open File With Partial Name Filter") MyFile = Dir("S:\Forms Folder\" & "*" & Ans & "*.xls") MyFilter = path & "*" & Ans & "*.xls" With Application.FileDialog(msoFileDialogOpen) .AllowMultiSelect = False .InitialFileName = MyFilter If .Show = 1 Then MyFile = .SelectedItems(1) End If End With On Error Resume Next Set WB = Workbooks.Open(MyFile) End Sub 

这将是一个肮脏的黑客,但如果你在这里有一个Else分支:

 If .Show = 1 Then MyFile = .SelectedItems(1) Else MyFile = vbNullString End If 

实际打开文件的代码可以在尝试以下操作之前validationMyFile是否为空:

 On Error Resume Next If MyFile <> vbNullString Then Set WB = Workbooks.Open(MyFile) 

这就是说,我认为你应该在这里处理至less错误53(“找不到文件”),而不是仅仅在地毯下推出所有的错误。

此外, WB参考不被使用。 也许Sub应该是一个返回已打开的工作簿的Function ,或者如果打开失败,则返回Nothing

这是我用来select一个目录。 如果函数返回一个空string,我不会尝试打开文件。

 Private Function FolderPicker() As String '******************************************* ' returns directory path to be printed to ' does not allow multiple selections, ' so returning the first item in selected ' items is sufficient. ' ' returns empty string On Error or if the ' user cancels '******************************************** On Error GoTo ErrHandler Const DefaultDirectory As String = "C:Path\to\default\directory\" With Application.FileDialog(msoFileDialogFolderPicker) .AllowMultiSelect = False .Title = "Choose Directory to Print to" .InitialFileName = DefaultDirectory .InitialView = msoFileDialogViewSmallIcons If .Show <> -1 Then FolderPicker = vbNullString Else FolderPicker = .SelectedItems(1) End If End With Exit Function ErrExit: FolderPicker = vbNullString Exit Function ErrHandler: MsgBox "Unexpected Error: " & Err.number & vbCrLf & "Source: " & Err.Source & _ "Description: " & Err.Description, vbCritical, "ERROR!" Resume ErrExit End Function 

所以,你会这样称呼它。

 MyFile = FolderPicker If MyFile <> vbNullString Then Set WB = Workbooks.Open(MyFile) End If 

后来多了血,汗水和眼泪(严重的网上冲浪,拼凑代码和重新testing)我find了一个答案,没有任何问题,在任何时候按“取消”。

  Sub OpenAuditPartialName() ' Returns popup window with only filtered ' filenames matching input criteria. ' Filenames are saved from another code that uses 3 variables to generate a _ ' filename 'Filename part1_Filename part2_Filename part3 Forms.xls' Dim WB As Workbook Dim Ans As String Dim MyFile As String Dim path As String ' Folder path for Forms path = ("S:\Forms Folder\") Ans = InputBox("Enter any part of the filename to search by." & vbCrLf & vbCrLf & _ "Full or Partial information is OK." & vbCrLf & vbCrLf & "Filename part1" _ & vbCrLf & "Filename part2" & vbCrLf & "Filename part3", "Enter Partial Filename Filter") ' Exits on 'Cancel' as it should If Ans = "" Then Exit Sub End If MyFile = Dir(path & "*" & Ans & "*.xls") MyFilter = path & "*" & Ans & "*.xls" '******************************************* With Application.FileDialog(msoFileDialogOpen) .AllowMultiSelect = False .InitialFileName = MyFilter ' Now accepts the 'Cancel' instead of continuing to open the first file ' in the filtered list when pressed If .Show = 0 Then ElseIf Len(Ans) Then MyFile = .SelectedItems(1) On Error Resume Next Set WB = Workbooks.Open(MyFile) Else Exit Sub End If '******************************************* End With End Sub