Opening File in VBA via User Prompts

GeneralShamu

Board Regular
Joined
Jul 6, 2007
Messages
127
I have written code that will open an XLS file in a specific directory with a specific name...do it's thing and then close it.

Now it seems it'll be easier to just have the user decide since the file name will change based on the last updated date.

Any ideas?
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Try like this

Code:
Sub OpenFile()
Dim fName As Variant
fName = Application.GetOpenFilename(FileFilter:="Excel Files (*.XLS), *.XLS", Title:="Select File To Be Opened")
If fName = False Then Exit Sub
Workbooks.Open Filename:=fName
End Sub
 
Upvote 0
Try like this

Code:
Sub OpenFile()
Dim fName As Variant
fName = Application.GetOpenFilename(FileFilter:="Excel Files (*.XLS), *.XLS", Title:="Select File To Be Opened")
If fName = False Then Exit Sub
Workbooks.Open Filename:=fName
End Sub

How can I open it this way w/ "True" for UpdateLinks and ReadOnly?

"Workbooks.Open Filename:=fName, True, True" is throwing a compilation error.
 
Upvote 0
Try

Code:
Sub OpenFile()
Dim fName As Variant
fName = Application.GetOpenFilename(FileFilter:="Excel Files (*.XLS), *.XLS", Title:="Select File To Be Opened")
If fName = False Then Exit Sub
Workbooks.Open Filename:=fName, UpdateLinks:=True, ReadOnly:=True
End Sub
 
Upvote 0
Try

Code:
Sub OpenFile()
Dim fName As Variant
fName = Application.GetOpenFilename(FileFilter:="Excel Files (*.XLS), *.XLS", Title:="Select File To Be Opened")
If fName = False Then Exit Sub
Workbooks.Open Filename:=fName, UpdateLinks:=True, ReadOnly:=True
End Sub

It still asked me to update links...

When I do

Workbooks(Filename).Close
and then
Workbooks(fName).Close

neither work for closing the workbook.

The first one did when I outright defined a directory and filename.
 
Upvote 0
Try like this

Code:
Sub OpenFile()
Dim fName As Variant, wb As Workbook
fName = Application.GetOpenFilename(FileFilter:="Excel Files (*.XLS), *.XLS", Title:="Select File To Be Opened")
If fName = False Then Exit Sub
Set wb = Workbooks.Open(Filename:=fName, UpdateLinks:=True, ReadOnly:=True)
'
'more code
'
wb.Close False
End Sub
 
Upvote 0
Try like this

Code:
Sub OpenFile()
Dim fName As Variant, wb As Workbook
fName = Application.GetOpenFilename(FileFilter:="Excel Files (*.XLS), *.XLS", Title:="Select File To Be Opened")
If fName = False Then Exit Sub
Set wb = Workbooks.Open(Filename:=fName, UpdateLinks:=True, ReadOnly:=True)
'
'more code
'
wb.Close False
End Sub


Thank you; still getting the message that one or more links cannot be updated.

Do you think putting this before the 'Set' line would alleviate this in all instances?
Code:
Application.DisplayAlerts = False
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,831
Members
452,947
Latest member
Gerry_F

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