OzGrid

Excel: Get File Name From User to Open Workbook Or Save Workbook

< Back to Search results

 Category: [Excel]  Demo Available 

Excel: Get File Name From User to Open Workbook Or Save Workbook

 

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:

Excel VBA: Code to Locate Two Matches in 2 Separate Columns
Excel VBA: Does Cell Have Formula
Excel VBA Macro: Determine Which Button, Control or Command Button Was Clicked
Excel VBA: Macro Code To Run Macros On Protected Worksheets & Sheets
Excel VBA: Stop Screen Flickering in Recorded Macros and Speed up Your Code
Excel Ranges: Finding the Last Cell in a Range

 

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.


Gallery



stars (0 Reviews)