Type Mismatch error when having user select files

z3115

Board Regular
Joined
Nov 1, 2013
Messages
71
Hello, I have the following code, which opens up a dialogue box and allows the users to select 1 or more files at once. If the user does not select any files (i.e. they hit cancel), I want the macro to end.

The code below works if the user does not select any files, however, if they do I get a "type mismatch" error.

The weird part is that if I completely remove the parts of the code that check to see if any files were selected, the rest of the code works fine (except for an error when the user does not select any files).

Is it related to the "Variant" variable type? By checking to see if it's false am I somehow forcing it to a boolean type, thus causing an error when the variable gets used later on in the code?

Thank you!

Code:
Sub multiSelectV3()    Dim myFile As Variant
    Dim i As Integer


    myFile = Application.GetOpenFilename(MultiSelect:=True)
    If myFile = False Then
        'Exit Sub
    Else
        For i = LBound(myFile) To UBound(myFile)
            MsgBox Dir(myFile(i))
            'Set wb = Workbooks.Open(myFile(i))
        Next i
    End If
End Sub
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Hello, I have the following code, which opens up a dialogue box and allows the users to select 1 or more files at once. If the user does not select any files (i.e. they hit cancel), I want the macro to end.

The code below works if the user does not select any files, however, if they do I get a "type mismatch" error.

The weird part is that if I completely remove the parts of the code that check to see if any files were selected, the rest of the code works fine (except for an error when the user does not select any files).

Is it related to the "Variant" variable type? By checking to see if it's false am I somehow forcing it to a boolean type, thus causing an error when the variable gets used later on in the code?

Thank you!

Code:
Sub multiSelectV3()    Dim myFile As Variant
    Dim i As Integer


    myFile = Application.GetOpenFilename(MultiSelect:=True)
    If myFile = False Then
        'Exit Sub
    Else
        For i = LBound(myFile) To UBound(myFile)
            MsgBox Dir(myFile(i))
            'Set wb = Workbooks.Open(myFile(i))
        Next i
    End If
End Sub
Try
Code:
myFile = Application.GetOpenFilename(, , , , True)
And remove your dir function and just use the array element
 
Last edited:
Upvote 0
Try
Code:
myFile = Application.GetOpenFilename(, , , , True)
And remove your dir function and just use the array element

Tried this, it gives me a type mismatch error (if the user selects any files). Specifically, the error is on the line " If myFile = False Then"

any ideas?
 
Upvote 0
SOLVED - for anyone that sees this in the future, using variant as the variable type will cause the variable to either become boolean (if no files are selected) or an array (if any files are selected...even if only a single file is selected, since I have multi-select set to true). The best way to check if any files were selected is to actually check whether the variable is an array or not.

So, replace
Code:
If myFile = False Then

with this:
Code:
If Not IsArray(myFile) Then

Basically, if the variable myFile is not an array, that implies no files were selected by the user (so the macro should end).
 
Upvote 0

Forum statistics

Threads
1,214,599
Messages
6,120,448
Members
448,966
Latest member
DannyC96

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top