Excel VBA打开文件(错误处理) [英] Excel VBA Open File (error handling)
本文介绍了Excel VBA打开文件(错误处理)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果用户没有找到要打开的文件,我试图解释一个案例:
Dim fn As String
fn = Application.GetOpenFilename(所有文件,,1,选择文件,,False)
如果fn = False然后
退出Sub
如果
当没有选择文件时,这样做是我想要的。 >
但是当用户 选择文件时,此If语句会创建一个错误。任何人都可以告诉我正确的方法来实现这一点?
解决方案
该函数不返回布尔值 False
,但字符串 False
,因为它将变量从函数转换为变量类型,所以你的测试应该是
如果fn =False然后
(更改说明 - 感谢@SiddharthRout)
I'm trying to account for a case when the user does not find a file to open:
Dim fn As String
fn = Application.GetOpenFilename("All Files,.", 1, "Select a file", , False)
If fn = False Then
Exit Sub
End If
This does what I want it to when a file is not chosen.
But when the user does choose a file, this If statement creates an error. Can anyone tell me the proper way to accomplish this?
解决方案
The function does not return the boolean False
, but the string "False"
, as it will cast the variant from the function into your variable type, so your test should be
If fn = "False" Then
(changed explanation - thanks @SiddharthRout)
这篇关于Excel VBA打开文件(错误处理)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文