+ Reply to Thread
Results 1 to 4 of 4

SaveAs via VBA code

Hybrid View

  1. #1
    Registered User
    Join Date
    03-08-2010
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    17

    SaveAs via VBA code

    I'm sure this is a very simple one for most people! I'm trying to run a save as from code, but has a variable filename and to a variable directory so needs user input.

    I have set the ChDir to the root directory and used the GetSaveAsFilename application to open the save as dialogue box. This opens up the Save As box, to the correct directory, with the filename in the correct box. However, when I hit save, the box closes, sub ends as planned but the file has not been saved.

    Any ideas?
    Cheers
    A

    ChDrive ("S")
    Private Sub SaveAs_Control_Sheet()
    
    ChDir ("S:\CLIENTS")
    MsgBox "Select the folder in which to save the new control Sheet", , "Control Sheet Setup"
    Application.GetSaveAsFilename
    
    End Sub

  2. #2
    Registered User
    Join Date
    03-24-2010
    Location
    Pune, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    36

    Lightbulb Re: SaveAs via VBA code

    Hi,

    Please try below code:

    'ChDrive ("S")
    Private Sub SaveAs_Control_Sheet()
        Dim fileSaveName
        ChDir ("S:\Clients\")
        MsgBox "Select the folder in which to save the new control Sheet", , "Control Sheet Setup"
        fileSaveName = Application.GetSaveAsFilename(FileFilter:="Excel Files (*.xls), *.xls")
        If fileSaveName <> False Then
            MsgBox "Save as " & fileSaveName
        End If
        ThisWorkbook.SaveAs fileSaveName
    End Sub
    Last edited by nchavan; 03-31-2010 at 06:09 AM. Reason: To modify code part
    Cheerz,
    ~Ne@l

    *********************************************************************
    Save a tree. Please think before you print this post and/or attachments.

  3. #3
    Valued Forum Contributor JeanRage's Avatar
    Join Date
    03-02-2009
    Location
    Nice, France
    MS-Off Ver
    Excel 2003
    Posts
    705

    Re: SaveAs via VBA code

    Hi,

    You right with the variables ...
    With your full path (i.e. including the drive letter) stored in the variable strwkbPath, and
    your file name stored in the variable strwkbName, you could use
    following intruction :

    ActiveWorkbook.SaveAs Filename:=strwkbPath & strwkbName & ".xls"
    HTH

  4. #4
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: SaveAs via VBA code

    Hi,
    GetSaveAsFileName display the standard dialog and get user input, but doesnot save anything. Use something like this:

    Do
        fName = Application.GetSaveAsFilename
    Loop Until fName <> False
    ActiveWorkbook.SaveAs Filename:=fName
    Eventually, instead of
    ActiveWorkbook.SaveAs Filename:=fName
    you may use

    ActiveSheet.SaveAs Filename:=fName
    to save only one sheet as different workbook


    @nchavan: In your code, if user select to Cancel it will save the file with name False.xls
    Also, if you use ThisWorkbook and the routine is in Add-in, it will save the add-in.
    If you are pleased with a member's answer then use the Star icon to rate it.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1