我想在Excel中创建一个提示,供用户选择并打开文件。但是,与此同时,我还要确保用户正在选择正确的文件。 excel应该在任何一种情况下退出。
我有以下代码满足第一个要求,但在第二个要求上需要一些帮助。
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
答案 0 :(得分:1)
Sub correctAnswer()
Dim strFileToOpen As Variant
Dim correctFileName As String
correctFileName ="将您想要的文件名放在这里"
strFileToOpen = Application.GetOpenFilename _
(标题:="请选择缺少费用文件打开")
如果strFileToOpen = False 或strFileToOpen<> correctFileName 然后
MsgBox"没有/错误的文件被选中。程序将退出。",vbExclamation,"糟糕!"
Application.DisplayAlerts = False
Application.Quit
退出子
否则
Workbooks.Open Filename:= strFileToOpen
MsgBox"感谢您选择正确的文件",vbExclamation,"谢谢!"
结束如果
End Sub