|
|
Title | Use VBA code and the Excel Save As dialog to get the name of a file in which to save a workbook |
Description | This example shows how to use VBA code and the Excel Save As dialog to get the name of a file in which to save a workbook. |
Keywords | VBA, Excel, Save As, GetSaveAsFilename |
Categories | Office, Files and Directories |
|
|
Use the Application object's GetSaveAsFilename function. This function returns a Variant. If the user cancels, the return values is False. Otherwise it is the name of the file the user selected.
|
|
Private Sub cmdGetSaveAsName_Click()
Dim file_name As Variant
' Get the file name.
file_name = Application.GetSaveAsFilename( _
FileFilter:="Excel Files,*.xls,All Files,*.*", _
Title:="Save As File Name")
' See if the user canceled.
If file_name = False Then Exit Sub
' Save the file with the new name.
If LCase$(Right$(file_name, 4)) <> ".xls" Then
file_name = file_name & ".xls"
End If
ActiveWorkbook.SaveAs Filename:=file_name
End Sub
|
|
|
|
|
|