VBA error in Excel 13: type mismatch

I am trying to open a file dialog in excel so the user can select a file. For some reason, I keep getting a runtime error after I select the file I need. 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 

An error pops up when I select a valid .xlsx file and the debugger says something is wrong on this line:

 If dartFile = False Then 

Any help would be appreciated.

+5
source share
3 answers

Avoid using Variant data types whenever possible.

 Dim dartFile As String 

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

This returns True in the immediate area when you exit the ESC dialog box:

 ?Application.GetOpenFilename() = "False" 

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

+4
source

The problem is that Application.GetOpenFilename returns a variant, and you specified your variable as a string. VBA cannot compare your string with a boolean type.

+5
source

Try:

 Dim dartFile As as Variant 
0
source

Source: https://habr.com/ru/post/1205368/


All Articles