Excel运行时错误13中的VBA:键入不匹配

时间:2022-08-18 17:03:22

I'm trying to open a file dialog box in excel so a user can select a file. For some reason I keep getting a run time error after I've selected the file I want. Here is the code:

我正在尝试在Excel中打开文件对话框,以便用户可以选择文件。出于某种原因,在我选择了我想要的文件后,我不断收到运行时错误。这是代码:

Dim dartFile As String

dartFile = Application.GetOpenFilename _
(Title:="Please choose DART output to open", _
FileFilter:="Excel Files *.xlsx* (*.xlsx*),")

If dartFile = False Then
    MsgBox "No file selected.", vbExclamation, "Sorry!"
    Exit Sub
Else
    'Run the rest of the Sub
End IF

The error pops up when ever I select a valid .xlsx file, and the debugger says there is something wrong with this line:

当我选择有效的.xlsx文件时会弹出错误,调试器说这行有问题:

If dartFile = False Then

Any help would be appreciated.

任何帮助,将不胜感激。

3 个解决方案

#1


4  

Avoid using Variant data types, whenever possible.

尽可能避免使用Variant数据类型。

Dim dartFile As String

That's a good declaration, dartFile is, after all, a String.

这是一个很好的声明,dartFile毕竟是一个String。

This returns True in the immediate pane, when you ESC out of the dialog:

当ESC退出对话框时,在立即窗格中返回True:

?Application.GetOpenFilename() = "False"

Just make False, "False", and you're done. ;)

只做假,“假”,你就完成了。 ;)

#2


5  

The problem is that Application.GetOpenFilename returns a variant and you've declared your variable as a string. VBA then can't compare your string with a boolean type.

问题是Application.GetOpenFilename返回一个变量,并且您已将变量声明为字符串。然后VBA无法将您的字符串与布尔类型进行比较。

#3


0  

Try:

尝试:

Dim dartFile As as Variant

#1


4  

Avoid using Variant data types, whenever possible.

尽可能避免使用Variant数据类型。

Dim dartFile As String

That's a good declaration, dartFile is, after all, a String.

这是一个很好的声明,dartFile毕竟是一个String。

This returns True in the immediate pane, when you ESC out of the dialog:

当ESC退出对话框时,在立即窗格中返回True:

?Application.GetOpenFilename() = "False"

Just make False, "False", and you're done. ;)

只做假,“假”,你就完成了。 ;)

#2


5  

The problem is that Application.GetOpenFilename returns a variant and you've declared your variable as a string. VBA then can't compare your string with a boolean type.

问题是Application.GetOpenFilename返回一个变量,并且您已将变量声明为字符串。然后VBA无法将您的字符串与布尔类型进行比较。

#3


0  

Try:

尝试:

Dim dartFile As as Variant