PDA

View Full Version : Excel VBA Macro To Open A File Through Browse Dialog Box



Safal Shrestha
04-05-2013, 11:43 AM
I need a macro for the following task.
1. ask for the file name through inputbox
2. open the file if the name is correct otherwise give an error msgbox.
3. the directory can be specified initially in the vba.

Thanks.

Admin
04-05-2013, 12:35 PM
Hi

Rather asking for the filename, let the user select the file.


Sub kTest()

Dim strFile As String

With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
.Title = "Select the file.."
.InitialFileName = "D:\" 'adjust the default folder
.Filters.Clear
.Filters.Add "Excel Files", "*.xls*"
If .Show = -1 Then
strFile = .SelectedItems(1)
Else
MsgBox "No file selected", vbInformation, "ExcelFox.com"
Exit Sub
End If

Workbooks.Open strFile, 0

End With

End Sub

Safal Shrestha
04-05-2013, 12:59 PM
It worked perfectly. Thanks admin