Unable to use VBA to open a file selected in the file dialog box.

helpneeded2

Board Regular
Joined
Jun 25, 2021
Messages
110
Office Version
  1. 365
Platform
  1. Windows
I am quite new to VBA and have tried a couple different examples for using VBA to open a file that is select via the file dialog box.

After I select the file, nothing happens -- the file does not open.

I can only guess that perhaps the code examples I have been looking at are old, and maybe there is newer code I need to use?

Here is the current code I am trying to use:

VBA Code:
Sub FileOpenDialogBox()

'Display a Dialog Box that allows to select a single file.
'The path for the file picked will be stored in fullpath variable
  With Application.FileDialog(msoFileDialogFilePicker)
        'Makes sure the user can select only one file
        .AllowMultiSelect = False
        'Filter to just the following types of files to narrow down selection options
        .Filters.Add "Excel Files", "*.xlsx; *.xlsm; *.xls; *.xlsb", 1
        'Show the dialog box
        .Show
        
        'Store in fullpath variable
        fullpath = .SelectedItems.Item(1)
    End With
    
    'It's a good idea to still check if the file type selected is accurate.
    'Quit the procedure if the user didn't select the type of file we need.
    If InStr(fullpath, ".xls") = 0 Then
        Exit Sub
    End If

    'Open the file selected by the user
    Workbooks.Open fullpath

End Sub
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
What happens if you change
VBA Code:
    If InStr(fullpath, ".xls") = 0 Then
        Exit Sub
    End If
to
VBA Code:
    If InStr(fullpath, ".xls*") = 0 Then
        Exit Sub
    End If
 
Upvote 0
What happens if you change
VBA Code:
    If InStr(fullpath, ".xls") = 0 Then
        Exit Sub
    End If
to
VBA Code:
    If InStr(fullpath, ".xls*") = 0 Then
        Exit Sub
    End If

I made the change but still no file opens.

Although I'm a VBA newbie, I also find it odd that there is no Dim statement to initialize the variable type for fullpath?
 
Upvote 0
Your current check is case sensitive, which could be the problem. Try:

VBA Code:
    If InStr(1, fullpath, ".xls", vbtextcompare) = 0 Then

instead of the current version.
 
Upvote 0
Solution
Your current check is case sensitive, which could be the problem. Try:

VBA Code:
    If InStr(1, fullpath, ".xls", vbtextcompare) = 0 Then

instead of the current version.
Oh... then magic happens!

Thank you Rory, this now works.
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,561
Members
449,089
Latest member
Motoracer88

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