Consulting

Results 1 to 6 of 6

Thread: Solved: Default file name in Save As dialog

  1. #1
    VBAX Regular
    Joined
    Aug 2007
    Posts
    6
    Location

    Solved: Default file name in Save As dialog

    I have created a user form in Excel. I added a button that will invoke the "Save As" dialog. I want to change the default file name to the contents of a particular cell plus the word "Original". Can this be done? I know it can be done without going through the Save As window, but we ned to change the location where the file will be saved each time so that is why I need the dialog box.

    Here is the code I am using to invoke the Save As function:

    [VBA] Private Sub CommandButton1_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
    [/VBA]
    Here is the code that will save the file with the file name as I want it but it does not go through the Save As dialog box:

    [VBA] Private Sub CommandButton3_Click()
    Dim fName As String
    fName = Range("A1").Value & " Original"
    ' Change directory to suit your PC, including USER NAME
    ChDir _
    "C:\"
    ActiveWorkbook.SaveAs Filename:=fName
    End Sub[/VBA]

    Somehow the functionality of the second needs to be added to the first. I have been unsuccessful trying to do just that.

    Thanks in advance for the help!

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Whitty,
    Welcome to VBAX,
    Add the filename at the start of the procedure
    [VBA]Private Sub CommandButton1_Click()
    Dim file_name As Variant
    Dim FName As String
    FName = Range("A1").Value & " Original"
    ' Get the file name.
    file_name = Application.GetSaveAsFilename(FName, _
    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
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I would use the folderpicker dialog to get the folder, otherwise they can change the filename

    [vba]

    Private Sub CommandButton1_Click()
    Dim file_name As Variant
    Dim FName As String
    FName = Range("A1").Text & " Original"
    ' Get the file name.
    With Application.FileDialog(msoFileDialogFolderPicker)
    .Show

    If .SelectedItems.Count = 1 Then
    file_name = .SelectedItems(1) & Application.PathSeparator & FName & ".xls"
    ActiveWorkbook.SaveAs Filename:=file_name
    End If
    End With
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    BTW,
    Please use a meaningful title for your questions. It permits searches and is more likely to get appropriate attention.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    VBAX Regular
    Joined
    Aug 2007
    Posts
    6
    Location
    Thanks MD for the code. It works like a champ! I also appreciate the gentle delivery of the forum etiquette tip. This was my first forum post. I'm sure I will have more. Thanks!!

    xld - I tried your code as well and I see your approach. That could work too, but I like just having the default file name changed and ready to be saved where I need to put the file. Thanks as well!!

  6. #6
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Glad to help. If your question is answered, you can mark it "solved" using the Thread Tools dropdown.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •