Got any Excel/VBA Questions? Free Excel Help
Retrieve a workbook name and file path:
At some time during VBA coding it may be necessary to retrieve a file name and it's full path. You could use an Inputbox, but you would be asking for problems (typos). The best way to do this is with the "GetOpenFileName" and "GetSaveAsFilename" dialog. This will open the standard Open or Save as dialog and allow you to retrieve the name and full path.
Sub RetrieveFileName() Dim sFileName As String 'Show the open dialog and pass the selected _ file name to the String variable "sFileName" sFileName = Application.GetOpenFilename 'They have cancelled. If sFileName = "False" Then Exit Sub MsgBox sFileName End Sub
Get the file path and name to save a Workbook as:
Sub ParseSaveAsName() Dim sFileName As String 'Show the open dialog and pass the selected file name to the String variable "sFileName" sFileName = Application.GetSaveAsFilename 'They have cancelled If sFileName = "False" Then Exit Sub ThisWorkbook.SaveAs sFileName End Sub
See also:
See also: Index to Excel VBA Code; Index to Excel Freebies; Lesson 1 - Excel Fundamentals; Index to how to… providing a range of solutions
Click here to visit our Free 24/7 Excel/VBA Help Forum where there are thousands of posts you can get information from, or you can join the Forum and post your own questions.