Open File prompts not working consistently

SueBK

Board Regular
Joined
Aug 12, 2014
Messages
114
I'm running a bunch of reports that come from a couple of different spreadsheets. I'm trying to create a command-button system to allow users to run these reports. I don't want to hard-code the master data sheets' locations or names, because they change over time. I found two macros that appear to do what I want by asking the user to nominate a workbook to use. BUT, I found when I put the two together one after the other in a macro they didn't work. So, I put the first one on a separate command button, meaning the user had to nominate the master data report, then run the new report creation. I call the second one at the start of the report creation macros, to pick up the nominate file. BUT, sometimes it works, sometimes it says "Method 'Open' of object 'Workbooks' failed". Why is this happening :rofl:?
Code:
Sub xUseThisFile()
'Prompt user to open file for reporting. Sets the file path needed in xOpenFile (called in each report creation macro).
Dim intChoice As Integer
Dim strPath As String
Dim varCellvalue As String
'only allow the user to select one file
Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = False
'make the file dialog visible to the user
intChoice = Application.FileDialog(msoFileDialogOpen).Show
'determine what choice the user made
If intChoice <> 0 Then
    'get the file path selected by the user
    strPath = Application.FileDialog( _
        msoFileDialogOpen).SelectedItems(1)
    'print the file path to cell A1
    Cells(1, 1) = strPath
End If
End Sub
Sub xOpenFile()
'Opens the file nominated in Cell A1 and goes to the first sheet
varCellvalue = Range("a1").Value
Workbooks.Open varCellvalue
Sheets(1).Select
End Sub
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Grrr. Apparently a corrupt file. No idea how or why that is happening. "Saved as" my nominated file and all is sweet.

<sigh> This stuff gives me a headache.
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,917
Members
452,366
Latest member
TePunaBloke

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