Jonathan George
Jonathan George

Reputation: 125

VBA in Excel Run Time Error 13: Type Mismatch

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:

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:

If dartFile = False Then

Any help would be appreciated.

Upvotes: 5

Views: 4422

Answers (3)

Mathieu Guindon
Mathieu Guindon

Reputation: 71167

Avoid using Variant data types, whenever possible.

Dim dartFile As String

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

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

?Application.GetOpenFilename() = "False"

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

Upvotes: 5

Gary's Student
Gary's Student

Reputation: 96753

Try:

Dim dartFile As as Variant

Upvotes: 0

Mr. Mascaro
Mr. Mascaro

Reputation: 2733

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.

Upvotes: 5

Related Questions