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.
Printable View
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.
Hi
Rather asking for the filename, let the user select the file.
Code: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
It worked perfectly. Thanks admin