简体   繁体   English

如果打开了不正确的文件,请退出Excel

[英]Exit Excel If Incorrect File Is Opened

I want to create a prompt in Excel for the user to select and open a file. 我想在Excel中创建一个提示,供用户选择并打开文件。 However, at the same time I also want to make sure that the user is selecting the correct file. 但是,与此同时,我也要确保用户选择了正确的文件。 The excel should exit in either of the cases. 在两种情况下,excel都应退出。

  1. No file is selected. 未选择文件。
  2. Or Incorrect file is selected. 或选择了不正确的文件。

I have the following code that fulfills the first requirement but need some assistance on the second one. 我的以下代码可以满足第一个要求,但在第二个方面需要一些帮助。

Dim strFileToOpen As Variant
strFileToOpen = Application.GetOpenFilename _
(Title:="Please Choose Missing Charges File To Open")
If strFileToOpen = False Then
MsgBox "No File Selected. Program Will Exit.", vbExclamation, "Oops!"
Application.DisplayAlerts = False
Application.Quit
Exit Sub
Else
Workbooks.Open Filename:=strFileToOpen
MsgBox "Thanks For Selecting The Correct File", vbExclamation, "Thanks!"
End If

Sub correctAnswer() 子correctAnswer()

Dim strFileToOpen As Variant 将strFileToOpen变暗为变体

Dim correctFileName As String 昏暗的correctFileName作为字符串

correctFileName = "Put your intended file name here" CorrectFileName =“在此处放置您想要的文件名”

strFileToOpen = Application.GetOpenFilename _ strFileToOpen = Application.GetOpenFilename _

(Title:="Please Choose Missing Charges File To Open") (标题:=“请选择要打开的丢失收费文件”)

If strFileToOpen = False Or strFileToOpen <> correctFileName Then 如果strFileToOpen = False 或strFileToOpen <> correctFileName然后

MsgBox "No/Wrong File Selected. Program Will Exit.", vbExclamation, "Oops!" MsgBox“选择否/错误文件。程序将退出。”,vbExclamation,“糟糕!”

Application.DisplayAlerts = False Application.DisplayAlerts = False

Application.Quit 申请退出

Exit Sub 退出子

Else 其他

Workbooks.Open Filename:=strFileToOpen Workbooks.Open文件名:= strFileToOpen

MsgBox "Thanks For Selecting The Correct File", vbExclamation, "Thanks!" MsgBox“感谢您选择正确的文件”,vbExclamation,“感谢!”

End If 万一

End Sub 结束子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM