Really simple - VB code for the open file dialogue box?

sebhaque

New Member
Joined
Jan 13, 2011
Messages
11
Hi all.

Having a bit of an issue with Excel 101. I'm devising a nifty little macro that pulls in some data from a series of spreadsheets. These are all identical in layout so the actual legwork is very easy - but I'm struggling to remember how to call up the "open file" dialogue box (i.e. the one that you get if you press Ctrl+O).

What I'd like to do is, when the user presses a button, the box pops up allowing them to select the spreadsheet they want to grab the data from. They click OK, the workbook opens up, Excel switches to the opened workbook and does all the laborious copy and pasting.

Thanks in advance.
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Try like this

Code:
Dim fname As Variant
fname = Application.GetOpenFilename()
If Not fname = False Then Workbook.Open (fname)
 
Upvote 0
Thanks for the speedy response VoG. The code does display the open file dialogue box, but whenever I click on an Excel file (or any file for that matter) I receive a runtime error '424' - object required. The debug points to the Workbook.Open (fname) at the end of your code - which, hovering over it, is showing that fname is indeed the full filepath to the given document. Is there an alternative to the Workbook.Open command?

EDIT: Never mind. Just needed an 's' on the end of "Workbook" in "Workbook.Open"

Thanks again for your quick reply and assistance!
 
Last edited:
Upvote 0
Apologies

Rich (BB code):
Dim fname As Variant
fname = Application.GetOpenFilename()
If Not fname = False Then Workbooks.Open fname
 
Upvote 0

Forum statistics

Threads
1,214,642
Messages
6,120,700
Members
448,979
Latest member
DET4492

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