Search code examples
excelvbasave-as

Save As To Prompt For Location But Use Filename From Cell


I got this code from a forum and it works but I need to tweak it to show me the "save as" box but I with the name already populated with my variable FName.

Can someone help me with this?

Sub Save_New()
Dim FName As String
Dim FPath As String
'FPath = "C:"
FName = Sheets("Sheet1").Range("A1").Text
ThisWorkbook.SaveAs Filename:=FName
End Sub

Solution

  • You can use the Application.GetSaveAsFilename method for that …

    Option Explicit
    
    Sub Save_New()
        Dim FName As String
        FName = Sheets("Sheet1").Range("A1").Text
    
        Dim DialogResult As Variant 'variant is needed because the dialog returns FALSE if users presses cancel.
        DialogResult = Application.GetSaveAsFilename(InitialFilename:=FName)
    
        If Not DialogResult = False Then
            ThisWorkbook.SaveAs Filename:=DialogResult
        Else
            'user clicked cancel
        End If
    End Sub